Ruby:块参数中出现意外分号
我刚开始学习Ruby。 我键入了这个例子 :
x = 10 5.times do |y; x| x = y puts "x inside the block: #{x}" end puts "x outside the block: #{x}"
我有一个错误:
hello.rb:3:语法错误,意外’;’,期待’|’ 5次做什么 X |
请向我解释一下这是什么意思? 正如我理解的那样,这段代码应该有效。
这是一个新的1.9结构 ,你使用的是1.8。
它也适用于lambdas(包括刺伤),这很好:
> x = 42 > love_me = ->(y; x) do * x = y * puts "x inside the block: #{x}" * end > 2.times &love_me x inside the block: 0 x inside the block: 1 > puts "x outside the block: #{x}" x outside the block: 42