ruby-changes:62398
From: nagachika <ko1@a...>
Date: Sun, 26 Jul 2020 18:32:53 +0900 (JST)
Subject: [ruby-changes:62398] 05c4c88767 (ruby_2_7): merge revision(s) 1b4d406e3a04032b6d01e92b6d184a16945c6ac3: [Backport #16498] [Backport #16914]
https://git.ruby-lang.org/ruby.git/commit/?id=05c4c88767 From 05c4c88767b54808cd1459a1d6da11179c422cfa Mon Sep 17 00:00:00 2001 From: nagachika <nagachika@r...> Date: Sun, 26 Jul 2020 18:32:36 +0900 Subject: merge revision(s) 1b4d406e3a04032b6d01e92b6d184a16945c6ac3: [Backport #16498] [Backport #16914] Hash#transform_values should return a plain new Hash [Bug #16498] diff --git a/hash.c b/hash.c index dd78d2d..4a3f2ff 100644 --- a/hash.c +++ b/hash.c @@ -1558,10 +1558,8 @@ rb_hash_new_with_size(st_index_t size) https://github.com/ruby/ruby/blob/trunk/hash.c#L1558 } 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); @@ -1571,6 +1569,13 @@ hash_dup(VALUE hash, VALUE klass, VALUE flags) https://github.com/ruby/ruby/blob/trunk/hash.c#L1569 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) { @@ -3205,7 +3210,7 @@ rb_hash_transform_values(VALUE hash) https://github.com/ruby/ruby/blob/trunk/hash.c#L3210 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, result); diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb index 7832466..738b058 100644 --- a/test/ruby/test_hash.rb +++ b/test/ruby/test_hash.rb @@ -1664,9 +1664,15 @@ class TestHash < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_hash.rb#L1664 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)) diff --git a/version.h b/version.h index 04dd454..d2cfe49 100644 --- a/version.h +++ b/version.h @@ -2,7 +2,7 @@ https://github.com/ruby/ruby/blob/trunk/version.h#L2 # define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR #define RUBY_VERSION_TEENY 1 #define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR -#define RUBY_PATCHLEVEL 114 +#define RUBY_PATCHLEVEL 115 #define RUBY_RELEASE_YEAR 2020 #define RUBY_RELEASE_MONTH 7 -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/