DEV Community

Cover image for What's up with the & in Ruby and some background on procs

Posted on

What's up with the & in Ruby and some background on procs

Scrolling through API docs and Stack Overflow threads on Ruby inevitably reveals a number of unexplained &s floating around. Being new to Ruby and engineering in general, I thought I'd dig into what the ampersand, known as the safe operator, is and does.

The safe operator calls .to_proc on whatever it proceeds. But what, praytell, is a proc and what does it mean to become one? Before we get into &, we have to briefly fall down the rabbit hole of the Proc class.


On the surface, this isn't too terribly complicated. An instance of the Proc class is simply an executable block of code that can be stored in a variable to be called later or even passed as an argument to a method. A block of code is a grouping of statements called by a method. It is surrounded by { } or the do end keywords; think about what comes after .each.

cube = { |i| i**3 }
# => 8
Enter fullscreen mode Exit fullscreen mode

Note the .call method needed to call the proc instance which can also be accomplished with the shorthand cube.(2) or cube[2].

As mentioned, procs can also be used to pass a block as an argument of a method.

def i_take_a_proc(proc_arg)
proc_arg = { puts "I'm a block being passed as an argument!" }
# => I'm a block being passed as an argument!
Enter fullscreen mode Exit fullscreen mode

Now we can take this new (micro)superpower and apply the safe operator. Remember the safe operator simply calls .to_proc on its operand.

def make_proc(&block)
hello = make_proc { |i| puts "#{i} says hello!" }"The Moon")
# => The Moon says hello!

Enter fullscreen mode Exit fullscreen mode

When Ruby sees the &, it expects a proc to follow. Since we provide a block, Ruby will try to coerce it into a proc by calling .to_proc on the block.

Special Behaviors of Procs

Now that we have a handle on what procs are, let's talk about some of the properties that make them such beautiful and unique snowflakes. First off, procs (and blocks) are a type of closure, meaning that they remember the environment in which they are created. They essentially form a snapshot of the variables in their scope and will use that snapshot when they are called even if those variables have been reassigned.

def run_proc(say_hi)
  hi = "Hello"
hi = "Hola"
say_hi = { puts hi }
# => Hola
Enter fullscreen mode Exit fullscreen mode

This will puts Hola to the screen NOT Hello because that was the value of the hi variable that our new proc had access to when it was created and - because procs are closures - it holds onto that assignment of the variable.

Another interesting feature of procs is how they handle an explicit return. Rather than simply returning from their own block they will return from the surrounding context as well.

proc_that_returns = { return "I'm Free!" }
Enter fullscreen mode Exit fullscreen mode

Running this code outside of a method will produce a LocalJumpError because it is trying to return out of the top-most context.

Finally, procs are surprisingly laidback objects in the Ruby world. They are comfortable ignoring nil values and are not picky about the number of arguments they are given.

proc_with_two_args = { |x, y| x.even? } 
=> true 
Enter fullscreen mode Exit fullscreen mode

This proc expects two arguments: |x, y|. Even though it only receives one, no ArgumentError is raised!

Lambdas, Briefly

It's hard to dive into procs without talking about lambdas, but I'm here to talk about the safe operator so I'll make it quick. A lambda is a type of proc with slightly different behaviors. Namely, they do not return out of the surrounding scope with an explicit return, and they are particular about the number of arguments they accept. That's all!

lambda_with_two_args = lambda { |x, y| x.even? } 
=> ArgumentError
Enter fullscreen mode Exit fullscreen mode

The Safe Operator, Finally

So, what does this all mean for our friend the safe operator, &? Well, using what we now know, we can employ it to the benefit of some nice syntactic sugar.

This is most commonly seen when & is called on a symbol in the argument of an enumerator.

animals = ["dog", "platypus", "scarab"]
=> [3, 8, 6] 
#This is equivalent to: {|animal| animal.length}
Enter fullscreen mode Exit fullscreen mode

This is a neat trick, but what's going on here? Well, the safe operator indicates that Ruby expects a proc to follow it. Instead, it finds a symbol (:length) so it calls to_proc on the symbol, resulting in something like this:

{ |a| a.send(self) }
Enter fullscreen mode Exit fullscreen mode

In this case, self is the symbol :length, so Ruby calls the length method on the object that is calling (&:length). To be fair, this is likely a gross oversimplification of what is actually going on, but it helps clear things up a bit.

Another bit of syntactic wizardry the safe operator offers is a kind of failsafe. Because the safe operator is comfortable with missing arguments and nil data, it can be used when nil data may be present and you don't want it to break your code.

array = [1, 2, 3, nil, 5] { |e| e.even? }
=> NoMethodError { |e| e&.even? }
[false, true, false, nil, false] 
Enter fullscreen mode Exit fullscreen mode

This is a bit cleaner than writing lengthy if statements to check if all your data exist before using it. That said, it should be used carefully if having nil values in your data will be a major problem later. It can also be used when chaining method calls together. The safe operator will return nil when it finds the first nil object.

 #No safe operator :(
if genre && genre.artist && genre.artist.album
#With safe operator
if genre&.artist&.album
Enter fullscreen mode Exit fullscreen mode

Wrap up

Safe operators and procs may not change your life or even your code, but they can clean things up a bit. At the very least, they make it look like you know what you're doing (maybe not always a good thing).

Top comments (0)