When To Use Single Vs. Double Splats In Ruby

Published on

Use a single splat to pass an array of arguments to a method.

args = ['Rami', 'J', 'Massoud']

def say_name(given_name, middle_initial, family_name)
  puts "#{family_name}, #{given_name}, #{middle_initial}"
end

say_name(*args)

Use a double splat to pass a hash of arguments to a method with keyword arguments

args = [middle_initial: 'J', family_name: 'Massoud', given_name: 'Rami']

def say_name(given_name:, middle_initial:, family_name:)
  puts "#{family_name}, #{given_name}, #{middle_initial}"
end

say_name(**args)