Hey, Ruby — What’s the deal with Procs and Lambdas?
If you’ve been working with Ruby for a while, chances are you’ve heard of Procs, Lambdas, and anonymous functions. What are they, and why would you want to use them? Do even you need them?

So, to answer the last question first, no, you probably don’t need them, but they’re a neat feature of Ruby and do offer some stylistic advantages. Let’s back up a bit.
Procs and Lambdas are examples of closures — ways we package or group code. In Ruby, the most common closure is the block. We can pass a block (only one block!) to a method. Take everybody’s favorite Ruby module, enumerable:

Blocks, Procs, and Lambdas are all closures, but only Procs and Lambdas are objects. A method may only accept one block, but can be written to accept any number of Procs. Methods can also return a Proc or a Lambda as well. They can’t do that with blocks.
What’s the difference?
There are a few differences, and for the sake of simplicity, I’d recommend you stick with Lambdas.
“Proc” is short for “procedure,” an is many ways very similar to a method or function. Procs don’t strictly enforce the arguments you pass in, which can be a good thing or a bad thing, depending on the way you look at it. Lambdas in this way function more like Ruby methods — if you pass the wrong number of arguments into your Lambda, you’ll get an argument error.
There’s a major difference in how Procs and Lambdas handle return statements. Lambdas handle return the way you’re used to — you exit out of the closure. Procs, on the other hand return from the method enclosing the Proc! A picture is worth a thousand words.

See that? Weird, right? By now you might understand why I might recommend the use of Lambdas over Procs. Lambdas behave in a way which seems more familiar, very similar to a regular Ruby method, in fact.
So What?
Ok so why not use a method? Why go through the trouble of learning Procs and Lambdas? If you’ve been fine without using Procs for this long, why start now? The answer is that you probably don’t need them. They’re syntactic sugar for what you can get with a regular old Ruby method. There are a couple situations where they could be valuable, though, and we talked about them before. Unlike JavaScript, Ruby methods cannot be passed as arguments to other methods, nor can they return methods. Ruby methods can also accept only one block. Procs and Lambdas can provide a solution to this. If you’re a JavaScript programmer, you’re probably already in the habit of passing and returning functions to and from other functions, so the use of Procs and Lambdas can help to reduce the cognitive shift which occurs when switching languages.
Finally, “Anonymous Functions”
Procs and Lambdas provide a neat way to do a ‘one-off’ method, but sometimes, to me at least, the syntactic difference between them and a standard Ruby method is so small that I just go ahead and write out a method definition. You never know when you might need it again, right?
Sometimes, there really are times when you just want a “throw away” method. Enter the “Anonymous Function,” aka the “Stabby Lambda” or “Pointy Proc.”

As you can see, it’s a Lambda, so you won’t have any unexpected behaviors if you use return, but you will have strict argument enforcement so don’t try and do “-> (a, b){a * 2}[5]” without expecting it to blow up.
The “stabby lambda” or anonymous function, while not essential, does fill that niche need — you have some calculations which you need to perform, yet it’s not complicated or important enough to warrant defining a new method.
Wrapping Up
Do you need Procs, Lambdas, or anonymous functions? A case could be made either way. Even still, they’re definitely cool, and the “Stabby Lambda” provides a syntactically sweet way of using them. Plus, you can now safely drop “anonymous lambda function” into conversation. That alone is worth the five minutes you spent reading this, right?