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

ruby-changes:59669

From: Nobuyoshi <ko1@a...>
Date: Fri, 10 Jan 2020 21:47:43 +0900 (JST)
Subject: [ruby-changes:59669] 1b4d406e3a (master): Hash#transform_values should return a plain new Hash

https://git.ruby-lang.org/ruby.git/commit/?id=1b4d406e3a

From 1b4d406e3a04032b6d01e92b6d184a16945c6ac3 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Fri, 10 Jan 2020 21:26:43 +0900
Subject: Hash#transform_values should return a plain new Hash

[Bug #16498]

diff --git a/hash.c b/hash.c
index 4f72b4c..f68eaaa 100644
--- a/hash.c
+++ b/hash.c
@@ -1549,10 +1549,8 @@ rb_hash_new_with_size(st_index_t size) https://github.com/ruby/ruby/blob/trunk/hash.c#L1549
 }
 
 static VALUE
-hash_dup(VALUE hash, VALUE klass, VALUE flags)
+hash_copy(VALUE ret, VALUE hash)
 {
-    VALUE ret = hash_alloc_flags(klass, flags,
-				 RHASH_IFNONE(hash));
     if (!RHASH_EMPTY_P(hash)) {
         if (RHASH_AR_TABLE_P(hash))
             ar_copy(ret, hash);
@@ -1562,6 +1560,13 @@ hash_dup(VALUE hash, VALUE klass, VALUE flags) https://github.com/ruby/ruby/blob/trunk/hash.c#L1560
     return ret;
 }
 
+static VALUE
+hash_dup(VALUE hash, VALUE klass, VALUE flags)
+{
+    return hash_copy(hash_alloc_flags(klass, flags, RHASH_IFNONE(hash)),
+                     hash);
+}
+
 VALUE
 rb_hash_dup(VALUE hash)
 {
@@ -3213,7 +3218,7 @@ rb_hash_transform_values(VALUE hash) https://github.com/ruby/ruby/blob/trunk/hash.c#L3218
     VALUE result;
 
     RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
-    result = hash_dup(hash, rb_cHash, 0);
+    result = hash_copy(hash_alloc(rb_cHash), hash);
 
     if (!RHASH_EMPTY_P(hash)) {
         rb_hash_stlike_foreach_with_replace(result, transform_values_foreach_func, transform_values_foreach_replace, 0);
diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb
index 733bccd..ef32cff 100644
--- a/test/ruby/test_hash.rb
+++ b/test/ruby/test_hash.rb
@@ -1674,9 +1674,15 @@ class TestHash < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_hash.rb#L1674
 
   def test_transform_values
     x = @cls[a: 1, b: 2, c: 3]
+    x.default = 42
     y = x.transform_values {|v| v ** 2 }
     assert_equal([1, 4, 9], y.values_at(:a, :b, :c))
     assert_not_same(x, y)
+    assert_nil(y.default)
+
+    x.default_proc = proc {|h, k| k}
+    y = x.transform_values {|v| v ** 2 }
+    assert_nil(y.default_proc)
 
     y = x.transform_values.with_index {|v, i| "#{v}.#{i}" }
     assert_equal(%w(1.0  2.1  3.2), y.values_at(:a, :b, :c))
-- 
cgit v0.10.2


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

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