Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Documentation for Hash#merge and shallow copies. #228

Closed
wants to merge 1 commit into from

3 participants

Yorick Peterse Zachary Scott Benoit Daloze
Yorick Peterse

The documentation of Hash#merge has been updated to clarify that this method
creates a shallow copy of the Hash specified in the method parameter.

Sidenote: if there is a particular set of standards I violated I'd love to hear so I can adjust the code to these standards.

Yorick Peterse YorickPeterse Documentation for Hash#merge and shallow copies.
The documentation of Hash#merge has been updated to clarify that this method
creates a shallow copy of the Hash specified in the method parameter.

Signed-off-by: Yorick Peterse <yorickpeterse@gmail.com>
1b79d8f
Benoit Daloze eregon commented on the diff
@@ -1893,6 +1893,20 @@ static NOINSERT_UPDATE_CALLBACK(rb_hash_update_block_callback)
* h2 = { "b" => 254, "c" => 300 }
* h1.merge!(h2) { |key, v1, v2| v1 }
* #=> {"a"=>100, "b"=>200, "c"=>300}
+ *
+ * Note that this method creates a shallow copy of the value in
Benoit Daloze Collaborator

the values, no?

Zachary Scott Collaborator

Whoops, must have missed this one, do you want to commit this one Mr. Daloze?

Benoit Daloze Collaborator

OK, I'll take care of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Zachary Scott zzak referenced this pull request from a commit
Zachary Scott zzak * hash.c (rb_hash_update): Documentation for Hash#merge and shallow
  copies Patch by Yorick Peterse [Fixes Github #228]


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

Hi Yorick, Thanks for your contribution, and Happy New Year!

We have some guidelines on the wiki, but not all of the core classes follow the same style, it's sort of a work in progress ;)

Benoit Daloze
Collaborator

Hash#merge does not copy values, it just copies references, but that is transparent to the user.
So the same values are in the merged Hash:

> h = { a: 1 }
> v = [1, 2, 3]
> v.object_id
=> 2152765440 
> h.merge({ key: v })[:key].object_id
=> 2152765440 
Benoit Daloze
Collaborator

And @zzak, you are welcome to revert the commit ;)
(timing is quite unlucky, you commented between my 2 comments)

Happy new year though!

Zachary Scott
Collaborator

@eregon Thanks for clearing that up, I've reverted the docs in r38674.

Zachary Scott zzak closed this
Benoit Daloze
Collaborator

@YorickPeterse So, thanks for the request, but unfortunately it is misleading as is. But the doc of Hash#merge could likely be improved, do not hesitate to make another request!

Sandor Szücs szuecs referenced this pull request from a commit in szuecs/ruby
Zachary Scott zzak * hash.c (rb_hash_update): Revert documentation from r38672
  See: ruby#228 (comment)


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38674 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
663613a
Aaron Patterson tenderlove referenced this pull request from a commit in tenderlove/ruby
Zachary Scott zzak * hash.c (rb_hash_update): Documentation for Hash#merge and shallow
  copies Patch by Yorick Peterse [Fixes Github #228]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38672 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
613f49f
Aaron Patterson tenderlove referenced this pull request from a commit in tenderlove/ruby
Zachary Scott zzak * hash.c (rb_hash_update): Revert documentation from r38672
  See: ruby#228 (comment)


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38674 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
82a05e9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 01, 2013
  1. Yorick Peterse

    Documentation for Hash#merge and shallow copies.

    YorickPeterse authored
    The documentation of Hash#merge has been updated to clarify that this method
    creates a shallow copy of the Hash specified in the method parameter.
    
    Signed-off-by: Yorick Peterse <yorickpeterse@gmail.com>
This page is out of date. Refresh to see the latest.
Showing with 14 additions and 0 deletions.
  1. +14 0 hash.c
14 hash.c
View
@@ -1893,6 +1893,20 @@ rb_hash_update_block_i(VALUE key, VALUE value, VALUE hash)
* h2 = { "b" => 254, "c" => 300 }
* h1.merge!(h2) { |key, v1, v2| v1 }
* #=> {"a"=>100, "b"=>200, "c"=>300}
+ *
+ * Note that this method creates a shallow copy of the value in
Benoit Daloze Collaborator

the values, no?

Zachary Scott Collaborator

Whoops, must have missed this one, do you want to commit this one Mr. Daloze?

Benoit Daloze Collaborator

OK, I'll take care of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ * <i>other_hash</i>. This means that when for example
+ * <code>Array#select!</code> is used on one of the values in
+ * <i>other_hash</i> both the original object as well as the copy will
+ * be modified. This is illustrated in the following example:
+ *
+ * original = { 'numbers' => [10, 20, 30] }
+ * copy = {}.merge(original)
+ *
+ * copy['numbers'].select! { |number| number <= 20 }
+ *
+ * puts copy # => { 'numbers' => [10, 20] }
+ * puts original # => { 'numbers' => [10, 20] }
*/
static VALUE
Something went wrong with that request. Please try again.