gpt4 book ai didi

ruby - 如何在 Ruby 中定义/命名 block ?

转载 作者:数据小太阳 更新时间:2023-10-29 06:51:39 26 4
gpt4 key购买 nike

numbers = 1..10
print numbers.map {|x| x*x}

# I want to do:
square = {|x| x*x}
print numbers.map square

因为语法更简洁。我有办法做到这一点,而不必使用 def + end?

最佳答案

square = proc {|x| x**2 }
print number.map(&square)

关于ruby - 如何在 Ruby 中定义/命名 block ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5315037/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com