That is the double splat operator which is available since Ruby 2.0.
It captures all keyword arguments (which can also be a simple hash, which was the idiomatic way to emulate keyword arguments before they became part of the Ruby language)
def my_method(**options)
puts options.inspect
end
my_method(key: "value")
The above code prints {key:value}
to the console.
Just like the single splat operator captures all regular arguments, but instead of an array you get a hash.
Real-life example:
For example in Rails the cycle
method looks like this:
def cycle(first_value, *values)
options = values.extract_options!
# ...
end
This method can be called like this: cycle("red", "green", "blue", name: "colors")
.
This is quite a common pattern: You accept a list of arguments and the last one is an options hash, which can be extract - for example - using ActiveSupport's extract_options!
.
In Ruby 2.0 you can simplify these methods:
def cycle(first_value, *values, **options)
# Same code as above without further changes!
end
Admittedly it's only a minor improvement if you are already using ActiveSupport but for plain Ruby the code gains quite a lot of conciseness.