Project

General

Profile

Actions

Bug #12106

closed

Behavior of double splatting of hashes with non symbol key is different according to splatted hash position

Added by pabloh (Pablo Herrero) about 8 years ago. Updated over 4 years ago.

Status:
Closed
Target version:
-
ruby -v:
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]
[ruby-core:73957]

Description

When doing double splatting with hash with non symbols keys you get different behaviors according to the position of the hash been splatted:

{a: 3, **{b: 1}, **{'b' => 1}}     # Works fine
{a: 3, **{1 => 1}, **{b: 1}}       # Works fine
{3 => 3, **{b: 1}, **{'b' => 1}}   # Works fine
{**{}, a: 3, **{b: 1}, **{1 => 1}} # Works fine

{**{b: 1}, a: 3, **{1 => 1}}       # TypeError: wrong argument type Fixnum (expected Symbol)
{**{'b' => 1}, **{c: 4}}           # TypeError: wrong argument type Fixnum (expected Symbol)
{**{c: 4}, **{'b' => 1}}           # TypeError: wrong argument type Fixnum (expected Symbol)
{**{c: 4}, a: 3, **{'b' => 1}}     # TypeError: wrong argument type Fixnum (expected Symbol)

Same thing happens when you double splat inside a message send:

puts(a: 3, **{b: 1}, **{'b' => 1})     # Works fine
puts(a: 3, **{1 => 1}, **{b: 1})       # Works fine
puts(3 => 3, **{b: 1}, **{'b' => 1})   # Works fine
puts(**{}, a: 3, **{b: 1}, **{1 => 1}) # Works fine

puts(**{b: 1}, a: 3, **{1 => 1})       # TypeError: wrong argument type Fixnum (expected Symbol)
puts(**{'b' => 1}, **{c: 4})           # TypeError: wrong argument type Fixnum (expected Symbol)
puts(**{c: 4}, **{'b' => 1})           # TypeError: wrong argument type Fixnum (expected Symbol)
puts(**{c: 4}, a: 3, **{'b' => 1})     # TypeError: wrong argument type Fixnum (expected Symbol)

What's basically going on is this: you can double splat hashes with no symbol keys all you want, only if the first value of the hash is a regular key (symbol or not) and not a splatted hash, or also a double splatted empty hash.

It feels strange that building the same hash in different orders yields so different behaviors.
Anyhow, I personally feel it should be a bug if you cannot splat a hash with no symbol keys into another one, whichever are the remaining values of the hash.

Updated by pabloh (Pablo Herrero) about 8 years ago

Small correction: for the 2nd, 3rd and 4th examples on each code block the error actually is "TypeError: wrong argument type String (expected Symbol)"

Updated by shyouhei (Shyouhei Urabe) about 8 years ago

  • Status changed from Open to Assigned
  • Assignee set to nobu (Nobuyoshi Nakada)

Updated by jeremyevans0 (Jeremy Evans) over 4 years ago

  • Status changed from Assigned to Closed

With the changes in #14183, TypeError is no longer raised in any of the examples, as non-Symbol keys can be used inside a hash that is double splatted.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0