Using Lambdas in Ruby

Did you know you can use lambdas as a kind of dynamic hash replacement? The truth is, lambdas are pretty cool once you start to investigate them. In this post we'll cover basic usage and then show you some cool lambda tricks.

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

Unlike Procs, lambdas enforce the correct number of arguments Unlike Procs, 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"

Lambdas support default arguments Lambdas support default arguments

The 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. The 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 lambda, the return statement just returns from the lambda. It's just like any other function.

When you use the return keyword inside of the lambda, it returns from the lambda When you use the return keyword inside of the lambda, it returns from the lambda

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.

Ruby lambdas are closures

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.

There are at least three ways to invoke a lambda in Ruby There are at least three ways to invoke a lambda in Ruby

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.

Here, the lambda pretends to be a hash. Here, the lambda pretends to be a hash.

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.

You can curry any proc in Ruby You can curry any proc in Ruby

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.

Stabby 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:

Ruby's stabby lambda syntax was introduced in version 1.9 Ruby's stabby lambda syntax was introduced in version 1.9

What to do next:
  1. Try Honeybadger for FREE
    Honeybadger helps you find and fix errors before your users can even report them. Get set up in minutes and check monitoring off your to-do list.
    Start free trial
    Easy 5-minute setup — No credit card required
  2. Get the Honeybadger newsletter
    Each month we share news, best practices, and stories from the DevOps & monitoring community—exclusively for developers like you.
    author photo

    Starr Horne

    Starr Horne is a Rubyist and Chief JavaScripter at When she's not neck-deep in other people's bugs, she enjoys making furniture with traditional hand-tools, reading history and brewing beer in her garage in Seattle.

    More articles by Starr Horne
    Stop wasting time manually checking logs for errors!

    Try the only application health monitoring tool that allows you to track application errors, uptime, and cron jobs in one simple platform.

    • Know when critical errors occur, and which customers are affected.
    • Respond instantly when your systems go down.
    • Improve the health of your systems over time.
    • Fix problems before your customers can report them!

    As developers ourselves, we hated wasting time tracking down errors—so we built the system we always wanted.

    Honeybadger tracks everything you need and nothing you don't, creating one simple solution to keep your application running and error free so you can do what you do best—release new code. Try it free and see for yourself.

    Start free trial
    Simple 5-minute setup — No credit card required

    Learn more

    "We've looked at a lot of error management systems. Honeybadger is head and shoulders above the rest and somehow gets better with every new release."
    — Michael Smith, Cofounder & CTO of YvesBlue

    Honeybadger is trusted by top companies like:

    “Everyone is in love with Honeybadger ... the UI is spot on.”
    Molly Struve, Sr. Site Reliability Engineer, Netflix
    Start free trial