The Strange Ruby Splat

This article has been republished on Monkey and Crow.

As of ruby 1.9, you can do some pretty odd things with array destructuring and splatting. Putting the star before an object invokes the splat operator, which has a variety of effects. First we’ll start with some very useful examples, then we will poke around the dark corners of ruby’s arrays and the splat operator.

Method Definitions

You can use a splat in a method definition to gather up any remaining arguments:

  def say(what, *people)
    people.each{|person| puts "#{person}: #{what}"}
  say "Hello!", "Alice", "Bob", "Carl"
  # Alice: Hello!
  # Bob: Hello!
  # Carl: Hello!

In the example above, what will get the first argument, then *people will capture however many other arguments you pass into say. A real world example of this can be found in the definition of Delegator#method_missing. A common ruby idiom is to pass a hash in as the last argument to a method. Rails defines an array helper Array#extract_options! to make this idiom easier to handle with variable argument methods, but you can actually get a similar behavior using a splat at the beginning of the argument list:

  def arguments_and_opts(*args, opts)
    puts "arguments: #{args} options: #{opts}"
  arguments_and_opts 1,2,3, :a=>5
  # arguments: [1, 2, 3] options: {:a=>5}

Now this example only works if you are guaranteed to pass in a hash at the end, but it illustrates that the splat does not need to always come at the end of a method’s parameters. There are also some other odd uses for the splat in method defitions, for instance this is valid:

  def print_pair(a,b,*)
    puts "#{a} and #{b}"
  print_pair 1,2,3,:cake,7
  # 1 and 2

Outside of letting you mimic javascript calling conventions, I’m not sure what the practical use is:

  function print_pair(a,b){ 
    console.log(a + " and " + b);
  print_pair(1,2,3, "cake", 7);
  //=> 1 and 2

Calling Methods

The splat can also be used when calling a method, not just when defining one. If you wanted to use the say method from above, but you have your list of people in an array, the splat can help you out:

  people = ["Rudy", "Sarah", "Thomas"]
  say "Howdy!", *people
  # Rudy: Howdy!
  # Sarah: Howdy!
  # Thomas: Howdy!

In this case, the splat converted the array into method arguments. It doesn’t have to be used with methods that take a variable number of arguments though, you can use it in all kinds of other creative ways:

  def add(a,b)
    a + b
  pair = [3,7]
  add *pair
  # 7

Array Destructuring

First of all, lets quickly cover a few things you can do without splatting:

  a,b = 1,2               # Assign 2 values at once
  a,b = b,a               # Assign values in parallel
  puts "#{a} and #{b}"
  # 2 and 1

With the above samples in mind, let’s try some fancier stuff. You can use splats with multiple assignment to extract various elements from a list:

  first, *list = [1,2,3,4]          # first= 1, list= [2,3,4]
  *list, last  = [1,2,3,4]          # list= [1,2,3], last= 4
  first, *center, last = [1,2,3,4]  # first= 1, center= [2,3], last=4
  # Unquote a String (don't do this)
  _, *unquoted, _ = '"quoted"'.split(//)
  puts unquoted.join
  # quoted

Array Coercion

If for some reason the previous examples seemed like great ideas to you, you’ll be thrilled to know that the splat can also be used to coerce values into arrays:

  a = *"Hello"  #=> ["Hello"]
  "Hello".to_a  #=> NoMethodError: undefined method `to_a' for "Hello":String
  a = *(1..3)   #=> [1, 2, 3]
  a = *[1,2,3]  #=> [1, 2, 3]

This can be a nice way to make sure that a value is always an array, especially since it will handle objects that do not implement to_a

The splat is a wily beast popping up in odd corners of ruby. I rarely actually use it outside of in method definitions and method calls, but it’s interesting to know that it is available. Have you found any useful idioms that make use of the splat? I would love to hear about them.

About these ads


Filed under development, ruby

29 responses to “The Strange Ruby Splat

  1. Paul

    Slight correction to the opening line..

    As of ruby 1.8 <– splat is available in 1.8.

  2. Adam Sanderson

    I should probably have been more specific, the splat operator has been around for a while, but I believe that some of the behavior I show didn’t exist in ruby 1.8. I could be wrong though.

  3. Brian Butz

    What won’t work in 1.8 is using the splat to convert to array:

    a = *”Hello” => “Hello”

    You can wrap an array around it to ensure you always have an array, though:

    a = *1 => 1
    a = [*1] => [1]
    a = [*[1,2]] => [1,2]

  4. You should mention #to_ary, which is used for implicit splatting of Objects in 1.9.

    • Adam Sanderson

      Is it? I get the following behavior (1.9.1 p378: I need to upgrade…):

      a = "Hello".to_ary #=> NoMethodError: undefined method `to_ary'

      • class Foo
        def initialize(x,y)
        @x, @y = x, y

        def to_ary
        [@x, @y]

        foo =,2)
        x,y = foo
        x # => 4
        y # => 2

        I probably should have said custom-implicit splat. :)

  5. banisterfiend

    Great article.

    A gotcha between 1.8 and 1.9 is the change in behaviour of *[]:
    j = *[] #=> nil
    j = *[] #=> []

    I also use the def meth(*) end syntax when calling super in a subclass:

    def meth(*) puts "subclass"; super; end

    I also use it in Procs to maintain 1.8 and 1.9 compatibility:

    proc { |x, *| puts x }.call(1, 2)

    Without the `*` the code sample above behaves differently in 1.8 vs 1.9

  6. rebo

    awesome article, nice to see the lesser known features of ruby fu. More like this please.

  7. You omitted these two:
    `rescue *exception_classes`
    `when *cases`

    • Adam Sanderson

      Thanks apeiros that’s a good point, I do like the when *cases , but I haven’t use rescue *exception_classes. I think I’ll need to do a follow up with those.

  8. avdi

    Most of this stuff is also available in 1.8.

    Also, it’s worth noting that * first looks for #to_ary, and then for #to_a when destructuring Objects (not just arrays).

  9. avdi

    Oh and as far as interesting uses of the splat, here’s an article I wrote a while back:

  10. Yup, only some of the examples won’t work in 1.8.

    Array Destructuring examples where splat operator is not last – syntax error is raised;
    Array Coercion `a = *”Hello”` doesn’t make String to Array, but keeps it as a String.

    But you can mimic it always like this anyway: a = ["Hello"] – it’s even 2 characters less than .to_a and 1 more when compared to *. Nevertheless i think that it’s more straightforward than using splat in this case.

  11. Here’s another gotcha that’s bitten me before:

    In Ruby 1.8: *'' #=> []
    and in 1.9: *'' #=> [""]

  12. Pingback: Ruby’s Unary Operators and How to Redefine Their Functionality

  13. TS

    you should mention that passing an array to a method with * will pass it as a reference.. e.g.:

    a = [1,2,3]
    def plus1(a)
    a.each_index |i|
    a[i] += 1

    => [2,3,4]

    => [3,4,5]

    • TS

      the array can be modified by the method call, and the modified array will be available after method termination

    • It doesn’t. It will splat it, but since plus1 takes only one argument then the array will be passed as is. The code will work the same if you lose the * (and if you actually check your code, then you can see that you didn’t use * anywhere in your code examples).

  14. Pingback: A Placeholder for Ruby Splat | HashCode

  15. There’s a similar behavior on Ruby that I named it (by myself) as ‘placeholder’ for this gotcha. Example:

    langs = ["java", "csharp", "ruby", "haskell" ]
    l1,(*),l3 = *langs #l1 = “java”, l2 = “ruby”

    I’ve written up about at

  16. Pingback: Tips of Feb.27 | brucexlin

  17. I like to do:
    NORTH, EAST, SOUTH, WEST = *1..4

  18. Pingback: » Bundler.require(*Rails.groups(:assets => %w(development test))) SnowCrash

  19. The splat operator can also convert an Array into a Hash where even indices become keys and odd indices become values.

    Hash[*array] # => {1=>2, 3=>4, 5=>6}

  20. Pingback: Parallel assignment in Ruby « brain dump

  21. Pingback: Дайджест интересных новостей и материалов из мира PHP за последние две недели (25.08.2013 — 08.09.2013) | Juds

  22. Pingback: PHP Digest №2 ( 25.08.2013 — 08.09.2013 ) | FASIGN Blog