【发布时间】:2010-11-28 23:24:57
【问题描述】:
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"
我认为 return 关键字在 Ruby 中是可选的,并且无论您是否请求,您始终是 returning。鉴于此,我发现 foo 和 bar 具有不同的输出,这是由 foo 在 Proc f 中包含显式 return 这一事实决定的,这让我感到惊讶。
有人知道为什么会这样吗?
【问题讨论】:
标签: ruby return proc-object