A Quick Look at Ruby blocks, procs, and lambdas

Blocks, Procs, Lambdas

Blocks

We can call a block using the yield keyword, like so:

def print_once
yield
end
print_once { puts “I ran.” }# => “I ran.”def print_twice
yield
yield
end
print_twice { puts “I ran.” }# => “I ran.”# => “I ran.”

Lastly, blocks can be given a name to explicitly pass it to another method, or to save as a variable for later use.

def explicit_block(&block)
block.call #same as yield
end
explicit_block { puts “I’m an explicit block!” }# => “I’m an explicit block!”

The &block parameter that may be making you scratch your head is simply the syntax used to define a blocks name!

Lambdas

greeting = -> { puts “Hello from a lambda!” }

…or like this:

greeting = lambda { puts “Hello from a lambda!” }

However, just like any other Ruby method, defining a lambda won’t run the code inside it. We need to use the call method for that:

greeting = -> { puts “Hello from a lambda!” }greeting.call# => “Hello from a lambda!”

We can also call a lambda with .(), [], and .===, but I personally feel that .call makes the code clearer and easier to understand. Lambdas can also take in arguments, and unlike procs (more on these further down the page), lambdas will throw an error if we pass the wrong number of arguments. Here is an example of a lambda defined with an argument:

greeting = ->(name) { puts “Hello, #{name}” }greeting.call(“Lebowski”)# => “Hello, Lebowski”

Procs

greeting = Proc.new { ‘Hello!’ }greeting.call# => ‘Hello!’

So here we created an instance of the Proc class with a block containing ‘Hello!’ and with the variable name greeting. We then use the .call method on the variable (which holds an instance of the Proc class) and see the return value of the block we passed in. Procs can also accept arguments that are passed into the .call method, like so:

greeting = Proc.new { |name| “Hello, #{name}!” }hello.call( “Lebowski” )# => “Hello, Lebowski!”

An interesting property of procs is that they don’t really care about the number of arguments. If you define a proc with one or many arguments and call it with none, it still runs!

There is much more that can be said about Ruby blocks, procs, and lambdas, but I hope this helps as a quick guide to get you familiar with the subject. Happy coding!

Full Stack Software Engineer | Audiophile

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store