|
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
|
the Whoops, must have missed this one, do you want to commit this one Mr. Daloze? 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
|
|
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
|
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
@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!
|
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
|
|
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
|
|
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
|
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>
@@ -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 | ||
the Whoops, must have missed this one, do you want to commit this one Mr. Daloze? 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 | ||
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.