In the last post I shared a bit of my understanding of what a Proc is. An anonymous block of code that can be represented as an object and invoked at will.
There are a lot of similarities when using Procs and Lambdas, in fact I’ve read that a lambda is just a particular type of Proc.
As shown below, setting up a lambda is included in the list of similarities:
Their are a few small but important differences I learned about between Procs and lambdas. The first being that when a Proc returns, it does so immediately, without returning to the calling method. lambdas on the other hand, will return control to the corresponding method. Another difference is that a lambda is particular about the number of parameters being passed to it.
This example comes from p. 417 of The Well Grounded Rubyist:
And that wraps up my elementary understanding of Procs and lambdas. If you have anything to add to this or any other posts, feel free to drop a line or more in the comments.