Bug #11501
closedAbout priority of a hash element when using {**hash1, **hash2} literal
Description
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 like 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 = { duplicate_key: 1, key: 1 }
=> {:duplicate_key=>1, :key=>1}
[2] pry(main)> hash2 = { duplicate_key: 3 }
=> {:duplicate_key=>3}
[3] pry(main)> { **hash1, **hash2 }
=> {:duplicate_key=>3, :key=>1}
[4] pry(main)>
- ruby 2.1.7p400 (2015-08-18 revision 51632) [x86_64-darwin13.0]
[1] pry(main)> hash1 = { duplicate_key: 1, key: 1 }
=> {:duplicate_key=>1, :key=>1}
[2] pry(main)> hash2 = { duplicate_key: 3 }
=> {:duplicate_key=>3}
[3] pry(main)> { **hash1, **hash2 }
=> {:duplicate_key=>1, :key=>1}
[4] pry(main)>
It seems that the behavior has changed from 2.2.0, however there is no mention in any documents as far as I searched.
Is this intended change?
Please check this and give me a opinion.
Warm Regards.
Updated by studio3104 (Satoshi SUZUKI) over 9 years ago
- Description updated (diff)
Updated by studio3104 (Satoshi SUZUKI) over 9 years ago
- Description updated (diff)
Updated by studio3104 (Satoshi SUZUKI) over 9 years ago
- Description updated (diff)
Updated by nagachika (Tomoyuki Chikanaga) about 9 years ago
- Status changed from Open to Assigned
- Assignee set to matz (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 = {a: 1, b: 2}
b = {a: 42}
p({ **a, **b })
results
→ 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=>1, :b=>2}
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin13.0]
{:a=>1, :b=>2}
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]
{:a=>1, :b=>2}
ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-darwin13.0]
{:a=>1, :b=>2}
ruby 2.1.4p265 (2014-10-27 revision 48166) [x86_64-darwin13.0]
{:a=>1, :b=>2}
ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-darwin14.0]
{:a=>1, :b=>2}
ruby 2.1.6p336 (2015-04-13 revision 50298) [x86_64-darwin14.0]
{:a=>1, :b=>2}
ruby 2.1.7p400 (2015-08-18 revision 51632) [x86_64-darwin14.0]
{:a=>1, :b=>2}
ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin13]
{:a=>42, :b=>2}
ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-darwin14]
{:a=>42, :b=>2}
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin14]
{:a=>42, :b=>2}
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin14]
{:a=>42, :b=>2}
ruby 2.3.0dev (2015-10-07 trunk 52068) [x86_64-darwin14]
{:a=>42, :b=>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 reasonable because it's consistent with Hash#merge.
If matz admit this change, I'd like to add "Hash's incompatible changes" section in NEWS file on ruby_2_2 branch.
Updated by wanabe (_ wanabe) about 9 years ago
git bisect
shows the change was introduced by #10315.
I guess it is intended.
Updated by wanabe (_ wanabe) about 9 years ago
- Related to Bug #10315: Override policy for duplicated keywords added
Updated by wanabe (_ wanabe) about 9 years ago
Oops, many revisions references #10315.
Sorry, r47878 is the revision.
Updated by nagachika (Tomoyuki Chikanaga) about 9 years ago
- Assignee changed from matz (Yukihiro Matsumoto) to nagachika (Tomoyuki Chikanaga)
- Backport changed from 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN to 2.0.0: DONTNEED, 2.1: DONTNEED, 2.2: REQUIRED
wanabe san, thank you for your investigation!
According to #10315, It's intentional change.
I'll add comment for it in NEWS in ruby_2_2 and doc/NEWS-2.2.0 in trunk.
Updated by nagachika (Tomoyuki Chikanaga) about 9 years ago
- Status changed from Assigned to Closed
Updated by nagachika (Tomoyuki Chikanaga) about 9 years ago
- Backport changed from 2.0.0: DONTNEED, 2.1: DONTNEED, 2.2: REQUIRED to 2.0.0: DONTNEED, 2.1: DONTNEED, 2.2: DONE
Add description to NEWS about this on ruby_2_2
branch (I committed in doc/NEWS-2.2.0 on trunk and backport it) at r53051.