我是新来的Ruby和感到惊讶的是这个片段raise
一个LocalJumpError
当块return
:
$bail_if = proc { |condition|
if condition
puts 'the condition is true'
return
else
puts 'the condition is false'
end
}
def method some_condition
$bail_if[some_condition]
end
method true
如果我将bail_if
定义为def method
中的局部变量,则重新是没有问题的。为什么是这样?
适用于红宝石1.8.7。你运行红宝石1.9吗? –
Woops,忘记提及我正在使用1.9.2p290。谢谢! – sivabudh
你问这是因为如果'some_condition'为true需要'方法'返回,但是否则继续运行'method'方法的其余部分?顺便说一下,'method'不是一个理想的名称,因为已经有[方法称为方法](http://www.ruby-doc.org/core-1.9.3/Object.html#method-i-方法) –