def foo
f = Proc.new { return "return from foo from inside proc" }
f.call # control leaves foo here
return "return from foo"
end
def bar
b = Proc.new { "return from bar from inside proc" }
b.call # control leaves bar here
return "return from bar"
end
puts foo # prints "return from foo from inside proc"
puts bar # prints "return from bar"
我认为关键字在 Ruby 中是可选的,并且无论您是否请求它return
,您总是会选择它。return
鉴于此,我发现令人惊讶的是,foo
并且具有不同的输出是由包含显式inbar
的事实决定的。foo
return
Proc f
有谁知道为什么会这样?