Ruby splat operator (*variable) is used to convert an array of elements into an argument list.

Example Usecase

Date is passed by the user in the format : 2013/04/03 (YYYY/MM/DD), and you convert this date string to an array of integers using split and map.

1
"2013/04/03".split('/').map(&:to_i)

which would give the output [2013,04,03].

Now to convert this into a Time object, we need to supply this array as arguments into the Time.new.

The format of which is

1
  new(year, month=nil, day=nil, hour=nil, min=nil, sec=nil, utc_offset=nil)

we can’t put the above array as such to Time.new.We need to change the array to arguments such that the first element would be marked as year, second one the month, and third one the day. Here is where we use, the splat operator.

1
2
3
4
5
Time.new *[2013,04,03]

# or

Time.new *"2013/04/03".split('/').map(&:to_i)

Ruby splat can be used to accept inputs to a method as an array

1
2
3
4
5
6
 def new_method foo, *bar
    puts foo.inspect
    puts bar.inspect
 end

 new_method 1, 2, 3, 4, 5, 6, 7

=>

1
2
     1
     [2, 3, 4, 5, 6, 7]

The rest of the inputs will get stored into the bar as an array. So, in cases where we don’t know how many inputs we are expecting ,we can use this operator.

Convert Hash to array and array to Hash

Splat operator can be used to convert array to hash, and hash to array.

1
a = *{:a => 1, :b => 2}

=>

1
2
[[:a,1], [:b,2]]

1
Hash[*a.flatten]

=>

1
 { :a => 1, :b => 2}

Double splat in ruby 2.0 +

I haven’t explored the full possibilities of this operator. The double spat can be used to store the hash that is passed into a method.

1
2
3
4
def method **foo
  puts foo.inspect
end
method a: 1, b: 2, c: 3

=>

1
 {:a=>1, :b=>2, :c=>3}

Example with all of them together

1
2
3
4
5
6
7
def method a, *b, **c
   puts a.inspect
   puts b.inspect
   puts c.inspect
end

method 1, 3, 2, 3, 5, 12, 12, s: 1, b: 1

=>

1
2
3
1
[3, 2, 3, 5, 12, 12]
{:s=>1, :b=>1}