What is a Proc, Anyway?
According to the Ruby documentation, “Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.” But what does that really mean?
Let’s do some experimentation with Procs.
1 2 3 4 5
So it looks like a proc is a set of instructions inside of a variable. If I run the
#call method on the proc, the arguments that I pass into
#call will be passed into the block. Since I entered
proc.call("hello!"), Ruby returned
proc.call("how's it going?") returns
"HOW'S IT GOING?".
Let’s try a longer one:
1 2 3 4 5 6 7 8
Again, whatever we pass into
#call gets passed into our block. This is starting to make sense!
Normalizing Ruby Blocks
One important use of procs is their ability to set a default block for a Ruby method. Consider the method
#do_something that takes a word and passes it into a block:
1 2 3 4 5 6
But what if we want
#do_something to capitalize its argument if if we don’t call it with a block. As it stands, we’ll get an error if we try to call
#do_something without a block:
If we pass in another argument that starts with
&, our block gets captured in that variable, as a proc. So we can rewrite our
#do_something method as:
1 2 3 4 5 6 7 8 9
Now, setting a default block is a breeze:
1 2 3 4 5 6 7 8 9 10 11
POWERFUL! Next time, we’ll use our newfound proc powers to build a sort.