Difference Between Procs and Lambdas

// Apr 2, 2015

Lambda

When you instantiate a lambda within a method, the lambda will perform it’s function in coherence with the method. So if you define a lambda:

def some_method
  my_lambda = lambda { return "Hello!" }.call
  "Goodbye"
end

some_method #=> "Goodbye"

Even though the lambda has a return statement, it will not exit the entire method but only return itself, and continue on with the method it exists in.

Proc

When you instantiate a Proc.new within a method, the proc becomes integrated as a part of the method:

def some_method
  my_proc = Proc.new { return "Hello!" }.call
  "Goodbye"
end

some_method #=> "Hello!"

Now when the method runs, once the proc is hit. The entire method will stop and return.