onruby_cool_kid.rb — Blocks, Proc and Lambdas the close siblings. Part 2

Pablo Adell
4 min readJan 25, 2022

--

Photo by Joshua Fuller on Unsplash

As we said in previous articles: Blocks in the end are just closures for chunks of code, not really objects.

They do not store any reference to the class that has been instantiated nor any instance variables or inheritance chains. They are just chunks of code bound to a context.

What could we possibly do to make it an object? So that we can use the same block multiple times by storing it into a variable, same as we do with objects?

Blocks in the end are just closures for chunks of code, not really objects.

They do not store any reference to the class that has been instantiated nor any instance variables or inheritance chains. They are just chunks of code bound to a context.

Here it comes the time for our two main characters to make the big entrance, Proc and lambda

Proc

What is a Proc, you might be asking?

It simply is an object that serves as an encapsulation of a block of code. By doing so, the block can be stored in a variable, passed to a method, or used in another Proc. It can also be called, executing the code inside the block.

Lambda

Now that we know what a Proc is. What is a lambda ?

Well, it simply is an object that serves as an encapsulation of a block of code.

This definition is the same as the Proc object, but lambdas have some specific nuances that must be taken into account:

  • If any arguments are specified, the lambda will require all of them in order to work, raising ArgumentError otherwise. In the case of a Proc , it will use nil if no arguments have been passed.

The number of arguments passed to a method is also know as arity . We could say that lambdas require their arity to be fulfilled whereas Proc do not.

  • Lambdas always return to its calling method, instead of returning immediately. Proc on the other hand, will return immediately if the block returns.

Let’s put this into code so we can better understand it:

What happens if no argument is passed

Notice how if an argument is not passed, the Proc object will use nil as its value, whereas lambda will raise ArgumentError exception.

Returning in Procs and lambdas

Notice how "I will not reach this part"is not printed, as the Proc object has returned immediately and the method will not execute any further.

However, by using the lambda the block returns to its calling method and it can continue executing, thus printing "I will reach this part"

That’s it for today folks!

However, we are not yet done, stick around for the last part of this saga, where we will extend the functionalities of Procs and lambdas .

For now, let’s extract the key points of this article:

  • Blocks are not really objects, they are not instantiate from any class, and so cannot be stored in variables. So we use Procs and lambdas to encapsulate them.
  • Procs and lambdas are similar in definition but differ in nuances like arity and returning behaviour
  • Procs do not care about arity (they use nil if argument is not present) and always return immediately
  • lambdas are strict about arity (raising ArgumentError if no argument is passed) and always return to its calling method

As usual, thanks a lot for taking time to read this article. Have a great day 😁

See you around 💃

Pablo.

--

--

Pablo Adell

Hello there!! My name is Pablo, I am a Full-Stack Software Engineer at Affirm, focused mainly in Ruby on Rails and React. I hope you enjoy my content!