[前][次][番号順一覧][スレッド一覧]

ruby-changes:26621

From: zzak <ko1@a...>
Date: Wed, 2 Jan 2013 02:17:38 +0900 (JST)
Subject: [ruby-changes:26621] zzak:r38672 (trunk): * hash.c (rb_hash_update): Documentation for Hash#merge and shallow

zzak	2013-01-02 02:17:28 +0900 (Wed, 02 Jan 2013)

  New Revision: 38672

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=38672

  Log:
    * hash.c (rb_hash_update): Documentation for Hash#merge and shallow
      copies Patch by Yorick Peterse [Fixes Github #228]

  Modified files:
    trunk/ChangeLog
    trunk/hash.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 38671)
+++ ChangeLog	(revision 38672)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Jan 02 02:16:00 2012  Zachary Scott  <zachary@z...>
+
+	* hash.c (rb_hash_update): Documentation for Hash#merge and shallow
+	  copies Patch by Yorick Peterse [Fixes Github #228]
+
 Mon Dec 31 15:10:00 2012  Zachary Scott  <zachary@z...>
 
 	* vm_backtrace.c: Add documentation for Kernel#caller_locations,
Index: hash.c
===================================================================
--- hash.c	(revision 38671)
+++ hash.c	(revision 38672)
@@ -1879,11 +1879,11 @@ rb_hash_update_block_i(VALUE key, VALUE https://github.com/ruby/ruby/blob/trunk/hash.c#L1879
  *     hsh.merge!(other_hash){|key, oldval, newval| block}    -> hsh
  *     hsh.update(other_hash){|key, oldval, newval| block}    -> hsh
  *
- *  Adds the contents of <i>other_hash</i> to <i>hsh</i>.  If no
- *  block is specified, entries with duplicate keys are overwritten
- *  with the values from <i>other_hash</i>, otherwise the value
- *  of each duplicate key is determined by calling the block with
- *  the key, its value in <i>hsh</i> and its value in <i>other_hash</i>.
+ *  Adds the contents of _other_hash_ to _hsh_.  If no block is specified,
+ *  entries with duplicate keys are overwritten with the values from
+ *  _other_hash_, otherwise the value of each duplicate key is determined by
+ *  calling the block with the key, its value in _hsh_ and its value in
+ *  _other_hash_.
  *
  *     h1 = { "a" => 100, "b" => 200 }
  *     h2 = { "b" => 254, "c" => 300 }
@@ -1893,6 +1893,19 @@ rb_hash_update_block_i(VALUE key, VALUE https://github.com/ruby/ruby/blob/trunk/hash.c#L1893
  *     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 _other_hash_.
+ *  This means that when for example Array#select! is used on one of the values
+ *  in _other_hash_ 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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]