Ruby single splat / double splat arguments

Splat operator or start (*) arguments in Ruby define they way they are received to a variable. Single splat operator can be used to receive arguments as an array to a variable or destructure an array into arguments. Double splat operator can be used to destructure a hash.

Single Splat (*) Operator

Single splat operator collects all the arguments in a variable as an array. It can be used to destructure an array values into variable(s).

Single splat operator in method definition

Let’s take an example.

def post_keywords(*keywords)
  puts "Class is: #{keywords.class}"
  puts "keywords are: #{keywords}"
end

We have defined a method post_keywords that receives arguments in a variable named keywords. The first line in the code prints class name of the variable keywords. Second line in the code prints values of keywords.

Let’s call the method defined above.

post_keywords(
      'Ruby Splat Operator',
      'Single Splat operator in Ruby',
      'How to gobble up values in Ruby'
      )

The result of method call above, it prints out values as given below.

Class is: Array
keywords are: Ruby Splat Operator
Single Splat operator in Ruby
How to gobble up values in Ruby

As we can see, single splat operator does following things.

  • Receive arguments (values) as an Array to a variable
  • Individual arguments can be accessed from array element
Single splat operator with multiple arguments

Single splat operator can be used with multiple arguments in ruby.

def post_keywords(site_name, *keywords)
  puts "Site Name is: #{site_name}"

  puts "Keywords are: #{keywords}"
end

This defined a method post_keywords, that accepts first argument as name of the site and second argument as keywords.

Now, we can call the method as given below.

post_keywords("Ruby in Rails", "Single splat operator", "Single splat with multiple arguments")

It will output as given below.

Site Name is: Ruby in Rails
Keywords are: ["Single splat operator", "Single splat with multiple arguments"]
  • We defined a method to receive site_name as a first argument
  • Rest of the arguments are gobbled up by splat operator in keywords variable
Single splat operator to destructure an array

Single splat operator can be used to destructure an array as given below. Let’s say, we have an array with first value as site name and rest of the arguments as site tags.

values = ['Ruby in Rails', 'Ruby', 'Rails']

Now, we can use single splat operator to destructure as given below.

site_name, *tags = values

> tags
[
    [0] "Ruby",
    [1] "Rails"
]

> tags
["Ruby", "Rails"]
[
    [0] "Ruby",
    [1] "Rails"
]

We can verify values to see that,

  • site_name has value Ruby in Rails
  • tags has value ["Ruby", "Rails"]

Double Splat (**) Operator

Double splat operator can be used to destructure a hash.

Let’s say we have a hash with keywords key with value of keywords for the post.

keyword_options = {
  keywords: ['Single Splat operator in Ruby', 'How to gobble up values in Ruby']
}

Another hash with site_name key as given below.

site_options = {
  site_name: 'Ruby Splat Operator',
  site_url: 'https://rubyinrails.com'
}

And we need to prepare a hash having key values from both hashes keyword_options and site_options.

It can be created with code given below.

all_options = { **keyword_options, **site_options }

all_options will have values as given below.

{
     :keywords => [
        [0] "Single Splat operator in Ruby",
        [1] "How to gobble up values in Ruby"
    ],
    :site_name => "Ruby Splat Operator",
     :site_url => "https://rubyinrails.com"
}
akshay

Akshay Mohite

Hi there! I am a Ruby on Rails & ReactJS Enthusiast, building some cool products at DTree Labs.

Read More
Buy me a coffee