Ruby Keyword Arguments and Defaults | Omar Bohsali

2018-11-09 16:23:06 +0000 -

Here is a powerful and concise Ruby pattern for dealing with splat keyword arguments. In this example, a and b are both required, and all other arguments live in rest.

def method(a:, b:, **rest)
  puts "a is #{a}, b is #{b}, rest is #{rest}"

>>> method({ a: '1', b: '2', c: '3', d: '4' })
a is 1, b is 2, rest is {:c=>"3", :d=>"4"}