Ruby Blocks, Procs, and Lambdas: What You Need to Know

Jacob Knopf
3 min readJun 20, 2020

When I first started learning about closures in Ruby I was pretty confused about what the differences were between code blocks, procs, and lambdas. After reading up on them a little more, however, I discovered they aren’t that confusing. In this post I want to give a brief overview of what each of them are, because you never know when they might come in handy. I personally haven’t used procs and lambdas much in my code so far, but they’re definitely worth knowing about. Let’s dive in!

A Ruby block is a selection of code inside a do..end statement, or in between curly braces {}. Blocks act like the body of a method, and are a way to group statements together to be called at a later time. You can also pass multiple variables into your block if you need to. Here’s an example of invoking a block.

def example_block_method
puts "You are now inside the method."
yield
puts "You are back inside the method again."
yield
end
example_block_method {puts "You are in the block now how cool!"}

This will result in the following output. Use the “yield” keyword to execute the block whenever you want.

=> You are now inside the method.
=> You are in the block now how cool!
=> You are back inside the method again.
=> You are in the block now how cool!

A proc on the other hand is a code block that’s stored in a local variable, not a method. Procs are Ruby objects that can be called later in a program and still have access to their variables from the context where they were defined initially. They can even be passed as arguments to methods just like other objects. Here’s an example of creating/calling a proc.

def example_proc(data)
Proc.new { |n| n * data } # remembers the value of data
end

multiply4 = example_proc(4)
multiply6 = example_proc(6)

multiply4.call(17) #=> 68
multiply6.call(12) #=> 72
multiply4.call(multiply6.call(9)) #=> 216

Finally we have lambdas, which are just a special type of proc. Both procs and lambdas are used in similar situations, and the main difference between the two is that lambdas behave more like regular methods. They care about the correct number of arguments and handle the return keyword the same way a normal method does. Conversely, procs immediately return from the current context/scope, and don’t evaluate any code after the proc’s return statement. You also won’t get an error if you pass the wrong number of arguments to a proc. Here’s an example of a lambda getting an argument error.

hello = lambda { |input| puts input } hello.call("It's nice to see you again.") #=> It's nice to see you again.hello.call #=> wrong number of arguments (given 0, expected 1) (ArgumentError)

Blocks, procs, and lambdas are very helpful tools in Ruby applications, and allow you to store data that can be used later when you need it. I hope this post illuminated some of the major differences between them, and how they work. You may not need to use procs or lambdas in your code tomorrow, but they are an integral part of Ruby programming. If you’re going to be applying for any backend developer positions, don’t go into an interview without a solid understanding of how closures work. Thanks for reading!

--

--

Jacob Knopf

QA Lead | Content Designer | UX Researcher | Developer Evangelist