Pregunta

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)

¿Fue útil?

Solución

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:

def foo (args)
  return args
end

h1 = { b: 2 }
h2 = { c: 3 }

foo(a: 1, **h1)       # => {:a=>1, :b=>2}
foo(a: 1, **h1, **h2) # => {:a=>1, :b=>2, :c=>3}
foo(a: 1, h1)         # Syntax Error: syntax error, unexpected ')', expecting =>
foo(h1, h2)           # ArgumentError: wrong number of arguments (2 for 1)

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.)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top