Skip to content

Commit 1290747

Browse files
author
zzak
committed
* 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
1 parent 10c8aa8 commit 1290747

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
Wed Jan 02 02:16:00 2012 Zachary Scott <zachary@zacharyscott.net>
2+
3+
* hash.c (rb_hash_update): Documentation for Hash#merge and shallow
4+
copies Patch by Yorick Peterse [Fixes Github #228]
5+
16
Mon Dec 31 15:10:00 2012 Zachary Scott <zachary@zacharyscott.net>
27

38
* vm_backtrace.c: Add documentation for Kernel#caller_locations,

hash.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1879,11 +1879,11 @@ rb_hash_update_block_i(VALUE key, VALUE value, VALUE hash)
18791879
* hsh.merge!(other_hash){|key, oldval, newval| block} -> hsh
18801880
* hsh.update(other_hash){|key, oldval, newval| block} -> hsh
18811881
*
1882-
* Adds the contents of <i>other_hash</i> to <i>hsh</i>. If no
1883-
* block is specified, entries with duplicate keys are overwritten
1884-
* with the values from <i>other_hash</i>, otherwise the value
1885-
* of each duplicate key is determined by calling the block with
1886-
* the key, its value in <i>hsh</i> and its value in <i>other_hash</i>.
1882+
* Adds the contents of _other_hash_ to _hsh_. If no block is specified,
1883+
* entries with duplicate keys are overwritten with the values from
1884+
* _other_hash_, otherwise the value of each duplicate key is determined by
1885+
* calling the block with the key, its value in _hsh_ and its value in
1886+
* _other_hash_.
18871887
*
18881888
* h1 = { "a" => 100, "b" => 200 }
18891889
* h2 = { "b" => 254, "c" => 300 }
@@ -1893,6 +1893,19 @@ rb_hash_update_block_i(VALUE key, VALUE value, VALUE hash)
18931893
* h2 = { "b" => 254, "c" => 300 }
18941894
* h1.merge!(h2) { |key, v1, v2| v1 }
18951895
* #=> {"a"=>100, "b"=>200, "c"=>300}
1896+
*
1897+
* Note that this method creates a shallow copy of the value in _other_hash_.
1898+
* This means that when for example Array#select! is used on one of the values
1899+
* in _other_hash_ both the original object as well as the copy will be
1900+
* modified. This is illustrated in the following example:
1901+
*
1902+
* original = { "numbers" => [10, 20, 30] }
1903+
* copy = {}.merge(original)
1904+
*
1905+
* copy["numbers"].select! { |number| number <= 20 }
1906+
*
1907+
* puts copy # => { "numbers" => [10, 20] }
1908+
* puts original # => { "numbers" => [10, 20] }
18961909
*/
18971910

18981911
static VALUE

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy