Blog

Thoughts from my daily grind

Ruby *Splat Operator in Detail

Posted by Ziyan Junaideen |Published: 01 May 2021 |Category: Ruby Language
Default Upload |

Ruby is a mature language with amazing syntactic sugar built to make programming fun and readable. One of the cool operators in Ruby is the splat operator (for example, *args). The splat operator has 2 main uses, which we will discuss in this post.

  1. to coercing arrays into a methods parameter list
  2. in a method to accept a variable number of arguments
  3. on method overrides, pass arguments to super

1. Coerce arrays into a methods parameter list

Assume you have a method that accepts many parameters. You can provide each parameter to the method making a huge method call. This is perfectly fine. But you can also have the arguments in an array and then provide them to the method using the splat operator.

def full_name(first_name, middle_name, last_name)
  "#{first_name} #{middle_name} #{last_name}"
end

args = %w[Mohamed Ziyan Junaideen]  # same as ["Mohamed", "Ziyan", "Junaideen"]

full_name(*args)                    # This is similar to calling
=> "Mohamed Ziyan Junaideen"        #   full_name("Mohamed", "Ziyan", "Junaideen")

Note: If the array you are coercing has a different number of elements than the number of method arguments, you will encounter an ArgumentError.

pry(main)> full_name(*%w[Ziyan Junaideen])
ArgumentError: wrong number of arguments (given 2, expected 3)

2. Methods to accept a variable number of arguments

Another useful use of the Ruby splat operator is to write methods that accept a variable number of arguments. There are 2 ways you can use the splat operator in a method argument list.

If you require to use the arguments within the method, you have to provide a label. Ruby will make the arguments from the position of the splat operator available as an array identified by the label.

def full_name(*fragments)
  fragments.join(' ')
end

full_name('Mohamed', 'Ziyan', 'Junaideen')
=> "Mohamed Ziyan Junaideen"

If you require to ignore the args, you can use the splat operator without using a label.

def full_name(*)
  # code that doesn't use provided args
end

3. Method override - pass arguments to super

Assume you have a method to override, and you require to call super. In this case, you can accept arguments with the splat operator. The Ruby interpreter will supply super within the method arguments without needing you to do so.

class Person
  def full_name(first_name, last_name)
    "#{first_name} #{last_name}"
  end
end

class Engineer < Person
  def full_name(*args)
    pp args
    "Eng. #{super}"  # note that the super method accepts 2 arguments
  end
end

Person.new.full_name('Ziyan', 'Junaideen')
=> "Ziyan Junaideen"

Engineer.new.full_name('Ziyan', 'Junaideen')
=> "Eng. Ziyan Junaideen"

Conclusion

The Ruby splat operator is a beneficial tool in your toolbox that address some common use cases in software development. I have seen many new and even some intermediate developers who don't utilize the splat operator.

I encountered the Ruby splat operator in my second week in Ruby land. I remember being confused why there was multiplication in a method argument list. I asked Bjoern Rennhak, the CTO of ClothesNetwork - present-day Orpiva.ai - the code's author about the splat operator. I got some links on the skype chat. It took some time to get accustomed to it. Practice is your best friend.

Tags
About the Author

Ziyan Junaideen -

Ziyan is an expert Ruby on Rails web developer with 8 years of experience specializing in SaaS applications. He spends his free time he writes blogs, drawing on his iPad, shoots photos.

Comments