If you work with Ruby, you’ve often encountered a situation where a method takes multiple keyword arguments. And you need to pass a hash to it and also add another new argument.
In most cases, you do the following: create a new hash and pass it as an argument to the method using the double splat operator **. But you can do this more concisely: instead of creating a new hash, pass the hash followed by another argument.
Furthermore, if you need a different order, you can pass the arguments in reverse order. If the argument keys are the same, the last argument will overwrite the first.
def some_method(**kwargs)
puts kwargs
end
# Passing arguments to a method using a new variable
hash = { a: 1, b: 2 }
new_hash = hash.merge(c: 3)
some_method(**new_hash) # {:a=>1, :b=>2, :c=>3}
# Direct transmission of keyword arguments
some_method(**hash, c: 3) # {:a=>1, :b=>2, :c=>3}
some_method(c: 3, **hash) # {:c=>3, :a=>1, :b=>2}
some_method(**hash, b: 3) # {:a=>1, :b=>3}
some_method(b: 3, **hash) # {:b=>2, :a=>1}