Possible Duplicate:
Using 'return' in a Ruby block
I’m new to Ruby, and was surprised that this snippet raise a LocalJumpError when the block 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
If I defined bail_if as a local variable in def method, then there is no problem. Why is this?
Change your
proc { |condition|tolambda { |condition|. proc-object has a block semantic, whereas lambda object has method semantic. Because the proc object is like a block, when you call a proc that executes a return statement, it attempts to return from the method that encloses the block that was converted to the proc. You don’t have such method in your first case and as consequence you getLocalJumpError. When you define your proc as a local variable in your method, all is working fine.