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中是可选的,无论你是否请求,你总是在输入return
.有鉴于此,我发现foo
和bar
有不同的输出是令人惊讶的,这是由foo
在Proc f
中包含显式return
这一事实决定的.
有人知道为什么会这样吗?