Hi all,
I’m new to the forum and the double-splat is difficult to search for, so
forgive me if this question has been addressed elsewhere.
I noticed what I find to be a very surprising behavior with the **
(double-splat) operator in Ruby 2.1.1.
When key-value pairs are used before a **hash, the hash remains
unmodified; however, when key-value pairs are used only after the
**hash, the
hash is permanently modified.
Here is the smallest example of this (UPDATE: I’m replacing an earlier
version):
h = { b: 2 }
{ a: 1, **h } # => { a: 1, b: 2 }
h # => { b: 2 }
{ a: 1, **h, c: 3 } # => { a: 1, b: 2, c: 3 }
h # => { b: 2 }
{ **h, c: 3 } # => { b: 2, c: 3 }
h # => { b: 2, c: 3 }
For comparison, consider the behavior of the single-* operator on
arrays:
a = [2]
[1, *a] # => [1, 2]
a # => [2]
[1, *a, 3] # => [1, 2, 3]
a # => [2]
[*a, 3] # => [2, 3]
a # => [2]
Is this behavior intentional? If so, where can I find the documentation
describing how the ** operator works?
I’ve fielded this question with the stackoverflow crowd as well:
Thanks,
-Jesse Sielaff