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/