diff --git a/hash.c b/hash.c index 4f72b4ca36..f68eaaabd4 100644 --- a/hash.c +++ b/hash.c @@ -1549,10 +1549,8 @@ rb_hash_new_with_size(st_index_t size) } 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) 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) 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 733bccd138..ef32cff868 100644 --- a/test/ruby/test_hash.rb +++ b/test/ruby/test_hash.rb @@ -1674,9 +1674,15 @@ class TestHash < Test::Unit::TestCase 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))