home  .   articles  .   til  .   about  .   contact

Ruby's Reverse Splat

Ruby’s splat operator is one nifty little feature. If you’re not familiar, the splat operator converts a variant number of arguments into an array like so…

def get_args *args

get_args('this', 'is', 'random')
=> ["this", "is", "random"]

The splat operator is super handy when the amount of arguments being passed is unknown. What I learned today was that this operator can also be used when passing an argument to a method. For a trivial example:

def stringify_address city, state, zip
  "#{city}, #{state} #{zip}"

my_add = ['Chicago', 'IL', 60626]

=> "Chicago, IL 60626"