Mixing arguments and keywords in Ruby

Tagged arguments, keywords, options, ruby, splat  Languages ruby

Mixing arguments and keywords in Ruby:

def hello(*args, **keywords)
  { args: args, keywords: keywords }

Splat to the rescue:

* turns all arguments into an array.

** turns all keyword arguments into a hash.

This allows you to do the following:

hello(:one, :two, { three: :four })
# or, simply
hello(:one, :two, three: :four)

=> {:args=>[:one, :two], :keyword_args=>{:three=>:four}}

Readability is improved by using proper names:

def hello(name, **options)
  { name: name, options: options }