Using Lambdas in Ruby
Blocks are such an important part of Ruby, it's hard to imagine the language without them. But lambdas? Who loves lambdas? You could go years without using one. They almost seem like a relic from a bygone age.
...But that's not quite true. Lambdas have some interesting tricks up their sleeves once you investigate them a little.
In this article we'll start with the basics of lambda usage, and then move on to some more interesting advanced usages. So if you use lambdas every day and know all about them, just scroll down.
The main thing to remember about Lambdas is that they act like functions. Blocks and Procs have their uses. But neither of them behaves 100% like a real life function. Lambdas do. Let's go down the list.
Lambdas enforce the correct number of arguments
Lambdas support default arguments
In the example below, we create a lambda function with a default argument value of "hello world"
return keyword works exactly how you'd expect.
Maybe this sounds like a small thing, but if you've ever tried to use return inside of a proc you know it's not.
The example below will show you what I mean. I've defined a method called
something that calls a
Proc has a return statement. But that return statement is tricky. Instead of just returning from the proc, it returns from the something method.
But when I do something similar with
return statement just returns from the lambda. It's just like any other function.
Lambdas are closures
To quote a great stack overflow post:
The most simple way to think of a closure is a **function that can be stored as a variable** (referred to as a "first-class function"), that has a special ability to access other variables local to the scope it was created in.
What does it mean to be able to access other variables local to the scope the lambda was created in? Let's take a look. In the example below I've created a local variable named
marco. I can use that variable inside of my lambda. And if I change the value in that variable, the lambda sees the new value.
Lambdas are chameleons
One of the truly weird things about lambdas is the variety of ways we can call them. The sample below shows three ways to invoke a lambda. They're all equivalent.
Perhaps this seems a little odd to you. It did to me at first. And to be honest the
l.(arg) syntax still baffles me. But the
l[arg] syntax is pretty interesting. It means that to a limited extent you can use lambdas in places where you'd normally use an array or a hash.
Using lambdas as computed hashes and arrays
Imagine that you have a test suite. As part of the initialization, you need to create fake Person records. It's simple. You just create a new FakePerson record and pass in a first name and a last name.
But what if you want to "fuzz test" the system by using different first and last names every time the test is run?
One way to do this might be to pass in a lambda function instead of a hash. In this example, I use a lambda function to generate fake names via the excellent Faker gem.
Lambdas have built-in currying
Currying is a cool technique used a lot in functional programming. It's a way to let you create new functions from existing functions. It's easy to understand when you see it in practice.
In the code below I have a lambda function that adds two numbers. Conveniently, it's called add. Then I use currying to create a more specialized function called increment. It simply adds one to any given number.
Lambdas are a special kind of Proc
You may have noticed that in all the code samples, whenever I've defined a lambda function, I get a Proc in return. That's because Ruby implements lambdas as a kind of Proc. So a lot of the things I've shown you in this article can be done with Procs as well as lambdas.
In this article I've used the lambda keyword for clarity. But there's a more concise syntax for defining lambdas introduced in Ruby 1.9 and known as the "stabby lambda." Here's how it works: