[ruby-dev:49288] [Ruby trunk - Bug #11501] [Assigned] About priority of a hash element when using {**hash1, **hash2} literal

From: nagachika00@...
Date: 2015-10-07 07:22:23 UTC
List: ruby-dev #49288
Issue #11501 has been updated by Tomoyuki Chikanaga.

Status changed from Open to Assigned
Assignee set to Yukihiro Matsumoto

Hello, satoshi san.
Thank you for your report.

I've checked the behaviors on each versions from 2.1.0-2.1.7, 2.2.0-2.2.4, =
and trunk(2.3-head).

script: hashes.rb
~~~
a =3D {a: 1, b: 2}
b =3D {a: 42}
p({ **a, **b })
~~~

results
~~~
=E2=86=92 for v in 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.2.0 2.=
2.1 2.2.2 2.2.3 2.3.0-dev; do rbenv shell $v; ruby -v hashes.rb; done
ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-darwin13.0]
{:a=3D>1, :b=3D>2}
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin13.0]
{:a=3D>1, :b=3D>2}
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]
{:a=3D>1, :b=3D>2}
ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-darwin13.0]
{:a=3D>1, :b=3D>2}
ruby 2.1.4p265 (2014-10-27 revision 48166) [x86_64-darwin13.0]
{:a=3D>1, :b=3D>2}
ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-darwin14.0]
{:a=3D>1, :b=3D>2}
ruby 2.1.6p336 (2015-04-13 revision 50298) [x86_64-darwin14.0]
{:a=3D>1, :b=3D>2}
ruby 2.1.7p400 (2015-08-18 revision 51632) [x86_64-darwin14.0]
{:a=3D>1, :b=3D>2}
ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin13]
{:a=3D>42, :b=3D>2}
ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-darwin14]
{:a=3D>42, :b=3D>2}
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin14]
{:a=3D>42, :b=3D>2}
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin14]
{:a=3D>42, :b=3D>2}
ruby 2.3.0dev (2015-10-07 trunk 52068) [x86_64-darwin14]
{:a=3D>42, :b=3D>2}
~~~

It seems that the behavior was changed between 2.1 <-> 2.2 series.
I don't known its intended or not.
In my personal opinion, I feel the latter (behavior of 2.2, 2.3) is reasona=
ble because it's consistent with Hash#merge.

If matz admit this change, I'd like to add "Hash's incompatible changes" se=
ction in NEWS file on ruby_2_2 branch.

----------------------------------------
Bug #11501: About priority of a hash element when using {**hash1, **hash2} =
literal
https://bugs.ruby-lang.org/issues/11501#change-54381

* Author: Satoshi SUZUKI
* Status: Assigned
* Priority: Normal
* Assignee: Yukihiro Matsumoto
* ruby -v: ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-darwin13.0]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
When creating a new hash using { **hash1, **hash2 } literal,
there are different behaviors between 2.2.0 or later and 2.1.7 or earlier l=
ike below.

* ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin13]
* ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-darwin13.0]

~~~
[1] pry(main)> hash1 =3D { duplicate_key: 1, key: 1 }
=3D> {:duplicate_key=3D>1, :key=3D>1}
[2] pry(main)> hash2 =3D { duplicate_key: 3 }
=3D> {:duplicate_key=3D>3}
[3] pry(main)> { **hash1, **hash2 }
=3D> {:duplicate_key=3D>3, :key=3D>1}
[4] pry(main)>
~~~

* ruby 2.1.7p400 (2015-08-18 revision 51632) [x86_64-darwin13.0]

~~~
[1] pry(main)> hash1 =3D { duplicate_key: 1, key: 1 }
=3D> {:duplicate_key=3D>1, :key=3D>1}
[2] pry(main)> hash2 =3D { duplicate_key: 3 }
=3D> {:duplicate_key=3D>3}
[3] pry(main)> { **hash1, **hash2 }
=3D> {:duplicate_key=3D>1, :key=3D>1}
[4] pry(main)>
~~~

It seems that the behavior has changed from 2.2.0, however there is no ment=
ion in any documents as far as I searched.

> * https://github.com/ruby/ruby/blob/v2_2_0/NEWS
> * https://www.ruby-lang.org/en/news/2014/12/25/ruby-2-2-0-released/

Is this intended change?
Please check this and give me a opinion.
Warm Regards.



--=20
https://bugs.ruby-lang.org/

In This Thread

Prev Next