I’ve always had a hard time understanding the differences between a block, a proc, and a lambda and an even harder time understanding when to use which. A friend of mine recommended reading a simple tutorial cleverly named “Understanding Ruby Blocks, Procs and Lambdas” written by Robert Sosinski. So far, this has been the most straightforward explanation of closures in Ruby that I have found, it doesn’t get too complicated and Robert provides detailed examples of the differences between these different types of closures in Ruby. Here are some of my takeaways:
- Block: A block is essentially a proc but cannot be saved as an object or assigned to a variable and is therefore a “one time” use solution. Also, a block itself cannot be passed to a method as an argument.
- Proc: A proc is a block that can be saved as an object, stored in a variable, and passed to a method as an argument. Since procs are objects, they can be reused in your code over and over again. Besides this obvious benefit, the real benefit comes when you need to pass more than one block to a method. Because procs are objects themselves, you can pass as many procs as you want as method arguments.
- Lambda: A lambda is like another way of writing methods, but anonymously. When writing code or going through the tutorial above, you’ll notice that using a lambda looks almost exactly like using a proc; at first it will seem like there isn’t any difference at all. This is why it is important to remember the conceptual differences between lambdas and procs; a lambda is like an anonymous method and a proc is a procudure, or a drop in code snippet, as Robert describes them. If this still doesn’t help you understand what a lambda is or what the differences are between a proc, don’t worry, it didn’t help me much either. Robert Sosinski provided some great examples describing the subtle differences though. Here’s what I took away from it all:
- Because lambdas are like methods, they check the number of arguments passed (unlike procs) and return an
ArgumentErrorin the case that the wrong number of arguments are passed. Procs on the other hand will simply set any extra arguments passed to
- A lambda will
returnit’s value to the method and let the method continue to process the rest of the code. However, if you instead had a proc within that same method, the proc will stop processing the rest of the method once it hits it’s
returnkeyword. Here’s the code snippet from Robert Sosinski’s tutorial:
- Lastly, because a lambda acts like a method, you can pass a lambda containing a
returnkeyword as an argument to another method. A proc containing a
returnkeyword however, cannot be passed as a method argument.
Conclusion: When to use
- Block: I’ve concluded from reading this tutorial that a block should be used over a proc when you simply want to break an object down and manipulate those elements in some sort of way.
- Proc: A proc should be used over a block when you want to reuse a block of code multiple times and when you have a method that needs to take more than one block as arguments.
- Lambda: I’ve concluded that the benefits of using a lambda over a proc (argument checking and return values) are minor and what you use will depend on your preference and what makes most sense to you.
What are your thoughts?