在Ruby 1.8中,proc/lambda与另一方面之间存在细微差别Proc.new.
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包含一个明确的return在Proc …
Ruby在通过Proc.new和lambda(或->()1.9中的运算符)创建的Proc之间存在差异.似乎非lambda Procs将在一个块参数中传递一个数组.通过lambda创建的过程不会.
p = Proc.new { |a,b| a + b}
p[[1,2]] # => 3
l = lambda { |a,b| a + b }
l[[1,2]] # => ArgumentError: wrong number of arguments (1 for 2)
有没有人对这种行为背后的动机有任何见解?