我想使用ablock
打印值5,如下所示。我尝试使用ablock {|x, y| puts "answer:#{x+2*y}"}
。但它没有在irb中显示任何内容。
def ablock
i = 1
j = 2
yield(i, j)
i = 3
j = 4
end
答案 0 :(得分:1)
它应该可以正常工作
2.1.1 :001 > def ablock
2.1.1 :002?> i = 1
2.1.1 :003?> j = 2
2.1.1 :004?> yield(i, j)
2.1.1 :005?> i = 3
2.1.1 :006?> j = 4
2.1.1 :007?> end
=> :ablock
2.1.1 :008 > ablock {|x, y| puts "answer:#{x+2*y}"}
answer:5
=> 4
2.1.1 :009 >