Project

General

Profile

Bug #16371

Inconsistent usage of Double splat operator

Added by dmytro.vasin (Dmytro Vasin) 10 months ago. Updated 9 months ago.

Status:
Open
Priority:
Normal
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) 10 months 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?

#2

Updated by sawa (Tsuyoshi Sawada) 10 months ago

  • Description updated (diff)

Updated by matz (Yukihiro Matsumoto) 10 months ago

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

Matz.

Updated by mame (Yusuke Endoh) 10 months 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"}
#5

Updated by hsbt (Hiroshi SHIBATA) 9 months ago

  • Tags set to backport

Also available in: Atom PDF