With a single splat, we can expand an array into multiple arguments, which is pretty different from passing the array directly:
def foo(a, b = nil, c = nil)
a
end
args = [1, 2, 3]
foo(args) # Evaluates to foo([1, 2, 3]) => [1, 2, 3]
foo(*args) # Evaluates to foo(1, 2, 3) => 1
With keyword arguments however, I can't see any difference, since they are just syntactic sugar to hashes:
def foo(key:)
key
end
args = { key: 'value' }
foo(args) # Evaluates to foo(key: 'value') => 'value'
foo(**args) # Evaluates to foo(key: 'value') => 'value'
Apart from the nice symmetry, is there any practical reason to use double-splats on method calls? (Note that this is distinct from using them on the method definition)
The example using a single argument is the degenerate case.
Looking at a nontrivial case, you can quickly see the advantage of having the new
**
operator:Using the
**
operator allows us to merge existing hashes (along with separate key-value arguments) at the call site, rather than adding another line (or more) of code to combine all the values into a single hash before passing it as an argument.(A piece of historical trivia—in Ruby 2.1.1, there was a bug where the splatted hash would be destructively modified, though it's since been patched.)