The Difference between Proc and Lambda in Ruby

April 25, 2011 · 0 comments

in Ruby

For those learning Ruby, the difference between a Proc and lambda might not be clear. A while back I read this somewhere which concisely demonstrates the difference. Here is the code snippet,

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

In short, calling a lambda via {lambda}.call() returns the control back to the caller even if the lambda block consists of a return statement.

Previous post:

Next post: