Project

General

Profile

Feature #15111

Make the number of arguments of `Hash#merge` variable

Added by liwii (Koki Ryu) 11 days ago. Updated 5 days ago.

Status:
Closed
Priority:
Normal
Target version:
-
[ruby-core:88970]

Description

Abstract

Make the number of arguments of Hash#merge variable.

Background

In many websites such as Stack Overflow and Qiita, many people are seeking how to merge more than three hashes.

https://stackoverflow.com/questions/19548496/how-to-merge-multiple-hashes-in-ruby
https://qiita.com/hc0208/items/c662f5189fa383872f4e
https://stackoverrun.com/ja/q/4997431

Many ways, like using Enumerable#inject or calling Hash#merge multiple times, are proposed, but both don't seem intuitive. Especially when using block in Hash#merge, the code becomes too complicated.

Proposal

Change the argument of Hash#merge from singular to variable length.

Implementation

https://github.com/ruby/ruby/pull/1951

Evaluation

The code to merge more than three hashes became much simpler and more intuitive.

before

hash1.merge(hash2).merge(hash3)

[hash1, hash2, hash3].inject do |result, part|
  result.merge(part) { |key, value1, value2| key + value1 + value2 }
end

after

hash1.merge(hash2, hash3)
hash1.merge(hash2, hash3) { |key, value1, value2| key + value1 + value2 }

Discussion

Summary

The change is needed to make Hash#merge more useful and intuitive.

Associated revisions

Revision 085f5ef9
Added by mame (Yusuke Endoh) 5 days ago

  • hash.c (rb_hash_merge): Accepts zero or more hashes as arguments

Hash#merge, merge!, and update could merge exactly two hashes.
Now, they accepts zero or more hashes as arguments so that it can merge
hashes more than two.

This patch was created by Koki Ryu liukoki@gmail.com at Ruby Hack
Challenge #5. Thank you!
[Feature #15111] [Fix GH-1951]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64777 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 64777
Added by mame (Yusuke Endoh) 5 days ago

  • hash.c (rb_hash_merge): Accepts zero or more hashes as arguments

Hash#merge, merge!, and update could merge exactly two hashes.
Now, they accepts zero or more hashes as arguments so that it can merge
hashes more than two.

This patch was created by Koki Ryu liukoki@gmail.com at Ruby Hack
Challenge #5. Thank you!
[Feature #15111] [Fix GH-1951]

Revision f7f216ed
Added by marcandre (Marc-Andre Lafortune) 5 days ago

[DOC] Add changes to Hash#merge in NEWS [#15111]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64780 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 64780
Added by marcandre (Marc-Andre Lafortune) 5 days ago

[DOC] Add changes to Hash#merge in NEWS [#15111]

History

#1 [ruby-core:88993] Updated by liwii (Koki Ryu) 10 days ago

If no argument is given to Hash#merge, the receiver itself is returned. In this way, Hash instance can revieve Array as arguments without concerning if the Array is empty.

hash1 = {a: 1, b: 2}
hashes = return_array_of_hashes
hash1.merge(*hashes) #=> It can't raise error even if `hashes` is empty.

#2 [ruby-core:89075] Updated by mame (Yusuke Endoh) 5 days ago

  • Assignee set to mame (Yusuke Endoh)
  • Status changed from Open to Assigned

As I recall, Matz has accepted this ticket at the day of developers' meeting. I'll commit it soon.

#3 Updated by mame (Yusuke Endoh) 5 days ago

  • Status changed from Assigned to Closed

Applied in changeset trunk|r64777.


  • hash.c (rb_hash_merge): Accepts zero or more hashes as arguments

Hash#merge, merge!, and update could merge exactly two hashes.
Now, they accepts zero or more hashes as arguments so that it can merge
hashes more than two.

This patch was created by Koki Ryu liukoki@gmail.com at Ruby Hack
Challenge #5. Thank you!
[Feature #15111] [Fix GH-1951]

Also available in: Atom PDF