Mixing arguments and keywords in Ruby
Mixing arguments and keywords in Ruby:
def hello(*args, **keywords)
{ args: args, keywords: keywords }
end
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 }
end