Project

General

Profile

Actions

Bug #16371

closed

Inconsistent usage of Double splat operator

Added by dmytro.vasin (Dmytro Vasin) over 4 years ago. Updated almost 3 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
2.6.0
[ruby-core:95951]

Description

Here is an issue with a weird behavior of ruby double splat operator:

a = {a: 'a'}
b = {'b' => 'b'}

{x: 'x', **a}
#=> {:x=>"x", :a=>"a"}

{x: 'x', **b}
#=> TypeError (hash key "b" is not a Symbol)

When I do that implicitly, it works:

{x: 'x', **{'b' => 'b'}} 
#=> {:x=>"x", "b"=>"b"}

At the same time:

{**{'b' => 'b'}}
# TypeError (hash key "b" is not a Symbol)

From my point of view, it definitely works inconsistently.

Could you help with this example or give an advice? (maybe I used it incorrectly?)

Updated by mame (Yusuke Endoh) over 4 years ago

** operator is for keyword arguments, and in Ruby 2.6, non-symbol key is not allowed in keyword arguments. So in principle, {x: 'x', **{'b' => 'b'}} should raise an exception.

Because of the spec change of keyword arguments (#14183), non-symbol key is allowed in Ruby 2.7, and all shown cases work without exception.

Do you want the case to raise an exception in Ruby 2.6?

Actions #2

Updated by sawa (Tsuyoshi Sawada) over 4 years ago

  • Description updated (diff)

Updated by matz (Yukihiro Matsumoto) over 4 years ago

I think this inconsistency is a bug. Double splat in hash expressions needs to work as interpolation.

Matz.

Updated by mame (Yusuke Endoh) over 4 years ago

matz, the following is the current behaviors. Do you mean which should be fixed?

(1) 2.7, via variable

b = {"b" => "b"}; p({x: "x", **b}) #=> current behavior: {:x=>"x", "b"=>"b"}

(2) 2.6, via variable

b = {"b" => "b"}; p({x: "x", **b}) #=> current behavior: hash key "b" is not a Symbol (TypeError)

(3) 2.7, with literal

p({x: "x", **{"b" => "b"}}) #=> current behavior: {:x=>"x", "b"=>"b"}

(4) 2.6, with literal

p({x: "x", **{"b" => "b"}}) #=> current behavior: {:x=>"x", "b"=>"b"}
Actions #5

Updated by hsbt (Hiroshi SHIBATA) about 4 years ago

  • Tags set to backport

Updated by jeremyevans0 (Jeremy Evans) almost 3 years ago

  • Status changed from Open to Closed

Ruby 2.6 is now in security maintenance mode, so behavior of the double splat operator will not be changed in it.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0