Procs! Procs! Procs! *Lil Jon Voice*

Bret Gibson
4 min readApr 9, 2020
Bet that song’s stuck in your head now… Good.

In one of my recent lectures, an instructor of mine used something called a Proc as an example of how to implement a certain functionality in his Ruby program. Given the fact that the Proc aspect of the implementation was not really the focus of the lecture, we did not really talk much about what it did. However, I was very intrigued as to how I could maybe use them in my own programs, so I decided to do some further research myself and write about my findings for other beginners looking to find information regarding Procs in Ruby.

Proc Basics

So, what is a Proc? Essentially, a Proc is a block of code that has been encapsulated into a variable. To create a Proc, you can declare it as such:

my_proc = Proc.new {puts "hello from a proc"}

Congratulations! You have now encapsulated a function into a variable in one line! Now, in order to run this function, we need to call it. This can be done by using the .call method:

OK, so that’s pretty cool but let’s get a little crazier. Inside of the Proc you can also pass in block variables, like so:

my_proc = Proc.new {|x| puts "hello from a proc " + x}

Now, when using the .call method, we can pass in a value which will then be utilized as the block variable “x” we declared above:

With these basics in mind, it’s time to consider one of the big strengths of Procs, the fact that they are closures. Objects that are closures are able to take with them and can utilize the context in which they were created. With this is mind, in the code below, what do you think will be output? “Inside of do_proc method” or “Outside of do_proc method?”

If you said, “Outside of do_proc method” you would be correct! This is because when we created the Proc, my_proc, the context in which it was created is outside of the method scope for the do_proc method. As such, my_proc takes the value of string_to_print variable (“Outside of do_proc method”) with it when it is passed into the do_pro method, so that the string_to_print that is inside of the do_proc method is unused.

This concept is so cool because we are able to use the variable that was in scope when the Proc was, which means we are able to access this variable even when the newly created Proc is in a different scope! Think of the possibilities!

Procs In Iterators

You can also use Procs with enumerable like .map or .each. To do this however, you must create a to_proc method to convert your object to a Proc. For example: here we have a class called Introducer, that has a to_proc method that creates a Proc that prints out a greeting, with the name of the person greeting themselves passed in through the block parameter.

We can then create an Introducer object and pass it as an argument into an enumerable using the notation “&object_variable_name”:

This will return:

Isn’t that awesome? The possibilities are endless with Procs!

I hope I have been able to teach you a little bit about how Procs work and maybe given you a greater desire to learn even more! I encourage you to try and utilize them in your own code!

--

--