Double-splat operator destructively modifies hash – is this a Ruby bug? -
i noticed find surprising behavior **
(double-splat) operator in ruby 2.1.1.
when key-value pairs used before **hash
, hash remains unmodified; however, when key-value pairs used after **hash
, hash permanently modified.
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 behavior of single-*
operator on arrays:
a = [2] [1, *a] # => [1, 2] # => [2] [1, *a, 3] # => [1, 2, 3] # => [2] [*a, 3] # => [2, 3] # => [2]
the array remains unchanged throughout.
do suppose sometimes-destructive behavior of **
intentional, or more bug?
in either case, documentation describing how **
operator meant work?
i asked question in ruby forum.
update
the bug fixed in ruby 2.1.3+.
the answers question seem be:
it's bug, rather intentional.
the behavior of
**
operator documented briefly in core library rdoc.
thanks suggestions of several commenters, i've posted bug ruby trunk issue tracker.
update:
the bug fixed in changeset r45724. comment there "keyword splat should non-destructive," makes authoritative answer.
Comments
Post a Comment