-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Make the number of arguments of Hash#merge
variable
#1951
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You also need to update the documentation, and clarify that the method is allowed to be called with zero argument by adding a test case.
hash.c
Outdated
if (rb_block_given_p()) { | ||
rb_hash_foreach(hash2, rb_hash_update_block_i, hash1); | ||
rb_hash_modify(self); | ||
for(int i = 0; i < argc; i++){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CRuby is written in C90, so you should stick to the old syntax here.
int i;
rb_hash_modify(self);
for (i = 0; i < argc; i++) {
hash.c
Outdated
rb_hash_modify(self); | ||
for(int i = 0; i < argc; i++){ | ||
VALUE hash = to_hash(argv[i]); | ||
if (rb_block_given_p()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you can memoize this value.
This proposal was accepted in yesterday's meeting, congratulations! |
@knu |
hash.c
Outdated
int i; | ||
|
||
rb_hash_modify(self); | ||
bool block_given = rb_block_given_p(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variable declarations are not allowed after a statement.
hash.c
Outdated
* More than one hash can be given to this method as arguments, then | ||
* the method returns a new hash containing the contents of the reciever | ||
* itself and all hashes given as arguments. The method also can be called | ||
* with no argument, then the receiver itself will be returned; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The result is a new hash, not the receiver itself.
@nobu |
Nice. As far as documentation goes, I'd keep it simple. I don't think that discussing the trivial case of no argument is necessary. The interface should be kept to a minimum by basically changing |
@marcandre |
hash.c
Outdated
bool block_given = rb_block_given_p(); | ||
|
||
rb_hash_modify(self); | ||
for(i = 0; i < argc; i++){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The indentation and style in block differ than others.
test/ruby/test_hash.rb
Outdated
@@ -1119,15 +1119,40 @@ def test_hash2 | |||
def test_update2 | |||
h1 = @cls[1=>2, 3=>4] | |||
h2 = {1=>3, 5=>7} | |||
h3 = {1=>1, 2=>4} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This variable is not used AFAIK.
@nobu |
@simi |
hash.c
Outdated
* | ||
* h1 = { "a" => 100, "b" => 200 } | ||
* h2 = { "b" => 254, "c" => 300 } | ||
* h3 = { "b" => 100, "d" => 400 } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed?
@mame |
Looks good. 👍 I'd even remove the part stating "The method also can be called with no argument, then nothing will change in the receiver.". It's important to test it, but it's not something people will use in general and should be obvious anyways. |
} | ||
else { | ||
rb_hash_foreach(hash, rb_hash_update_i, self); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
very trivial: These lines are indented with 3 spaces from for
loop, but maybe usually it's indented with 4 spaces
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. Thank you.
@liwii Merged! Thank you, and I expect you to continue contribution to Ruby 😃 |
As per Marc-Andre's comment. [Refs GH-1951] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64779 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Yes, I commented a few minutes before your commit. Cool, thanks! I added a note in the Congrats @liwii 🎉 |
… and `update` methods It follows Ruby 2.6 addition ruby/ruby#1951
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 callingHash#merge
multiple times, are proposed, but both don't seem intuitive. Especially when using block inHash#merge
, the code becomes too complicated.Proposal
Change the argument of
Hash#merge
from singular to variable length.Implementation
The code below.
Evaluation
The code to merge more than three hashes became much simpler and more intuitive.
before
after
Discussion
Summary
The change is needed to make
Hash#merge
more useful and intuitive.