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

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, and

Proc

What is a , 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 is. What is a ?

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 otherwise. In the case of a , it will use if no arguments have been passed.

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

  • always return to its calling method, instead of returning immediately. 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 object will use as its value, whereas will raise exception.

Returning in and

Notice how is not printed, as the object has returned immediately and the method will not execute any further.

However, by using the the block returns to its calling method and it can continue executing, thus printing

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 and .

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 and to encapsulate them.
  • and are similar in definition but differ in nuances like and
  • do not care about arity (they use nil if argument is not present) and always return immediately
  • are strict about arity (raising 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.

--

--

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!

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
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!