Ruby’s Splat Operator


Ruby is a dynamic language. One of the things it lets you do is define methods with an unknown (or variable) number of arguments. It does this using the splat operator. But the splat operator can actually be used for other things in your code, especially if you’re using Ruby 1.9. That’s because a small change to how splat operators work make them much more useful.

In the beginning

The humble splat operator was first used to slurp up unnamed arguments to a method:

def sum *numbers
  numbers.inject{|sum, number| sum += number}
end

Sometimes, this is just syntactic sugar, because passing a list of numbers like this:

sum(1, 2, 3)

Is more intuitive and prettier, and less error-prone, than passing them as an explicit array:

sum([1, 2, 3])

In many cases though, the splat operator is more than just a pretty face. Take Ruby’s own method_missing instance method, that is available in every class. If defined, it will attempt to handle any calls to methods that don’t explicitly exist:

class Person
  def method_missing sym, *args
    if sym.to_s =~ /^(\w+)=$/
      instance_variable_set "@#{$1}", args[0]
    else
      instance_variable_get "@#{sym}"
    end
  end
end

method_missing must be able to accept an unknown number of arguments, since just about any method call could be thrown at it. In this case, we’re using it to get and set instance variables without having to define them first using attr_accessor:

ruby-1.8.7-p299 > p = Person.new
 => #<Person:0x10018f640 @turtles=nil> 
ruby-1.8.7-p299 > p.turtles = 'I like them'
 => "I like them" 
ruby-1.8.7-p299 > p.turtles
 => "I like them" 
ruby-1.8.7-p299 > p.squirrels
 => nil 
ruby-1.8.7-p299 > p.squirrels = 'are okay'
 => "are okay" 
ruby-1.8.7-p299 > p.squirrels
 => "are okay" 

We’ve just created a class that lets us define any attributes we want, and our method doesn’t care whether *args contains zero arguments, or a hundred.

It gets unintentionally cooler

Up through Ruby 1.8 you could use this splat operator, in a limited fashion, for things other than method argument lists. You could use it to flatten an array, in contexts where it was the last element in the list:

ruby-1.8.7-p299 > last_numbers = [3, 4, 5]
 => [3, 4, 5] 
ruby-1.8.7-p299 > all_numbers = [1, 2, *last_numbers]
 => [1, 2, 3, 4, 5]

The splat operator took the last_numbers array and expanded it inline! Now our new array contains five numbers, instead of two numbers and a nested array. This comes in handy for meta-programming. So let’s try putting the splat operator somewhere else in the array:

ruby-1.8.7-p299 > middle_numbers = [2, 3, 4]
 => [2, 3, 4] 
ruby-1.8.7-p299 > all_numbers = [1, *middle_numbers, 5]
SyntaxError: compile error
(irb):4: syntax error, unexpected ',', expecting ']'
all_numbers = [1, *middle_numbers, 5]
                                  ^
	from (irb):4
ruby-1.8.7-p299 > first_numbers = [1, 2, 3]
 => [1, 2, 3] 
ruby-1.8.7-p299 > all_numbers = [*first_numbers, 4, 5]
SyntaxError: compile error
(irb):6: syntax error, unexpected ',', expecting ']'
all_numbers = [*first_numbers, 4, 5]
                              ^
	from (irb):6

We can’t use the splat operator anywhere else except the end of a list, just like in method calls. This really limits its value in Ruby 1.8.

Things get intentionally cooler

As of Ruby 1.9, however, the splat operator has been given a little more love, and now it can be used almost anywhere. Basically, any array that is given the splat operator will “flatten” itself, and return the list of elements NOT in an array. Now we can do cool stuff like this:

ruby-1.9.2-p0 > first_numbers = [1, 2, 3]
 => [1, 2, 3] 
ruby-1.9.2-p0 > middle_numbers = [4, 5, 6]
 => [4, 5, 6] 
ruby-1.9.2-p0 > last_numbers = [7, 8, 9]
 => [7, 8, 9] 
ruby-1.9.2-p0 > all_numbers = [*first_numbers, *middle_numbers, *last_numbers]
 => [1, 2, 3, 4, 5, 6, 7, 8, 9] 

This is handy for all sorts of meta-programming challenges – namely, handling dynamic argument lists. It’s also great when you’re building an array out of smaller pieces where some of the pieces are scalars (single values) and some are arrays. Let’s say we have a Family class, that contains myself, my parents, and my siblings. I want a method that returns everybody in one large array. The usage would look like this:

ruby-1.9.2-p0 > family = Family.new
 => #<Family:0x000001018ed2d8> 
ruby-1.9.2-p0 > family.myself = 'Greg'
 => "Greg" 
ruby-1.9.2-p0 > family.parents = ['Mike', 'Carol']
 => ["Mike", "Carol"] 
ruby-1.9.2-p0 > family.siblings = ['Peter', 'Bobby', 'Marsha', 'Jan', 'Cindy']
 => ["Peter", "Bobby", "Marsha", "Jan", "Cindy"] 
ruby-1.9.2-p0 > family.everybody
 => ["Greg", "Mike", "Carol", "Peter", "Bobby", "Marsha", "Jan", "Cindy"] 

If I want to do this without the splat operator (as I’d have to in Ruby 1.8) it would look like this:

class Family
  attr_accessor :myself, :parents, :siblings
  
  def everybody
    members = []
    members << myself
    members += parents
    members += siblings
  end
end

As you can see, I have to go line-by-line, interacting with the members array differently depending on if I’m adding one value or an array. Here’s how you’d do it without the splat operator:

class Family
  attr_accessor :myself, :parents, :siblings
  
  def everybody
    [myself, *parents, *siblings]
  end
end

Much cleaner! This version is shorter and easier to understand. This is a technique that might not seem useful, until you have a need for it. Then you’ll start finding uses for it everywhere.

About these ads

Tags: , , , , , ,

2 Responses to “Ruby’s Splat Operator”

  1. Craig S. Cottingham Says:

    I’ve been enjoying this series. Thanks for sharing.

    There was an interesting (at least to me) change recently in the way the splat is handled in method argument lists. In Ruby 1.8 and prior, only the last argument could be prefixed with a splat, in which case that variable received all remaining parameters. Starting with 1.9, *any* argument may be prefixed with a splat, so long as no more than one argument is so prefixed. Non-prefixed arguments get assigned parameters first, then the prefixed argument gets the rest, like so:

    def splat(a, *b, c)
    [ a, b, c ]
    end
    splat(1, 2, 3, 4) # => [ 1, [ 2, 3 ], 4 ]

    (Shamelessly copied from the 10th anniversary edition of Pickaxe, p. 335.)

    • Jaime Bellmyer Says:

      Hey Craig-

      Yeah, I love this! I guess I didn’t point that out explicitly in my article, although I did stumble on it during my research. While I can’t think of a practical use for it yet, it’s kind of cool knowing that you *can* :) And I got the 10th Anniversary pickaxe as well, their special price was too good to pass up. Step 2: reading it!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: