Proc.new vs Lambda in Ruby

March 19th, 2007

I found the following lines of code on Wikipedia today. It’s a very succinct description of one important difference between a lambda and a Proc. Try printing the return value of f.call for more insight.


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
  f = lambda { return "return from lambda" }
  f.call # control does not leave bar here
  return "return from bar" 
end

puts foo # prints "return from foo from inside proc" 
puts bar # prints "return from bar" 

Whole article on Wikipedia about closures. . . And a bunch more on procs and blocks

Sorry, comments are closed for this article.