ruby-changes:50718
From: naruse <ko1@a...>
Date: Thu, 22 Mar 2018 16:00:20 +0900 (JST)
Subject: [ruby-changes:50718] naruse:r62889 (ruby_2_5): merge revision(s) 62042, 62044: [Backport #14380]
naruse 2018-03-22 16:00:17 +0900 (Thu, 22 Mar 2018) New Revision: 62889 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=62889 Log: merge revision(s) 62042,62044: [Backport #14380] hash.c: support key swapping in Hash#transform_keys! * hash.c (rb_hash_transform_keys_bang): support key swapping in Hash#transform_keys! [Bug #14380] [ruby-core:84951] * test/ruby/test_hash.rb (test_transform_keys_bang): add assertions for this change Fix rubyspec against the change in Hash#transform_keys! [Bug #14380] [ruby-core:84951] Modified directories: branches/ruby_2_5/ Modified files: branches/ruby_2_5/hash.c branches/ruby_2_5/spec/ruby/core/hash/transform_keys_spec.rb branches/ruby_2_5/test/ruby/test_hash.rb branches/ruby_2_5/version.h Index: ruby_2_5/test/ruby/test_hash.rb =================================================================== --- ruby_2_5/test/ruby/test_hash.rb (revision 62888) +++ ruby_2_5/test/ruby/test_hash.rb (revision 62889) @@ -1573,6 +1573,14 @@ class TestHash < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/ruby_2_5/test/ruby/test_hash.rb#L1573 x.transform_keys!.with_index {|k, i| "#{k}.#{i}" } assert_equal(%w(a!.0 b!.1 c!.2), x.keys) + + x = @cls[1 => :a, -1 => :b] + x.transform_keys! {|k| -k } + assert_equal([-1, :a, 1, :b], x.flatten) + + x = @cls[true => :a, false => :b] + x.transform_keys! {|k| !k } + assert_equal([false, :a, true, :b], x.flatten) end def test_transform_values Index: ruby_2_5/version.h =================================================================== --- ruby_2_5/version.h (revision 62888) +++ ruby_2_5/version.h (revision 62889) @@ -1,6 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_5/version.h#L1 #define RUBY_VERSION "2.5.0" #define RUBY_RELEASE_DATE "2018-03-20" -#define RUBY_PATCHLEVEL 47 +#define RUBY_PATCHLEVEL 48 #define RUBY_RELEASE_YEAR 2018 #define RUBY_RELEASE_MONTH 3 Index: ruby_2_5/hash.c =================================================================== --- ruby_2_5/hash.c (revision 62888) +++ ruby_2_5/hash.c (revision 62889) @@ -1910,6 +1910,8 @@ rb_hash_transform_keys(VALUE hash) https://github.com/ruby/ruby/blob/trunk/ruby_2_5/hash.c#L1910 return result; } +static VALUE rb_hash_flatten(int argc, VALUE *argv, VALUE hash); + /* * call-seq: * hsh.transform_keys! {|key| block } -> hsh @@ -1933,12 +1935,14 @@ rb_hash_transform_keys_bang(VALUE hash) https://github.com/ruby/ruby/blob/trunk/ruby_2_5/hash.c#L1935 RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size); rb_hash_modify_check(hash); if (RHASH(hash)->ntbl) { - long i; - VALUE keys = rb_hash_keys(hash); - for (i = 0; i < RARRAY_LEN(keys); ++i) { - VALUE key = RARRAY_AREF(keys, i), new_key = rb_yield(key); - rb_hash_aset(hash, new_key, rb_hash_delete(hash, key)); - } + long i; + VALUE pairs = rb_hash_flatten(0, NULL, hash); + rb_hash_clear(hash); + for (i = 0; i < RARRAY_LEN(pairs); i += 2) { + VALUE key = RARRAY_AREF(pairs, i), new_key = rb_yield(key), + val = RARRAY_AREF(pairs, i+1); + rb_hash_aset(hash, new_key, val); + } } return hash; } Index: ruby_2_5/spec/ruby/core/hash/transform_keys_spec.rb =================================================================== --- ruby_2_5/spec/ruby/core/hash/transform_keys_spec.rb (revision 62888) +++ ruby_2_5/spec/ruby/core/hash/transform_keys_spec.rb (revision 62889) @@ -60,9 +60,9 @@ ruby_version_is "2.5" do https://github.com/ruby/ruby/blob/trunk/ruby_2_5/spec/ruby/core/hash/transform_keys_spec.rb#L60 @hash.should == { 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4 } end - it "does not prevent conflicts between new keys and old ones" do + it "prevents conflicts between new keys and old ones" do @hash.transform_keys!(&:succ) - @hash.should == { e: 1 } + @hash.should == { b: 1, c: 2, d: 3, e: 4 } end it "partially modifies the contents if we broke from the block" do @@ -70,7 +70,7 @@ ruby_version_is "2.5" do https://github.com/ruby/ruby/blob/trunk/ruby_2_5/spec/ruby/core/hash/transform_keys_spec.rb#L70 break if v == :c v.succ end - @hash.should == { c: 1, d: 4 } + @hash.should == { b: 1, c: 2 } end it "keeps later pair if new keys conflict" do Index: ruby_2_5 =================================================================== --- ruby_2_5 (revision 62888) +++ ruby_2_5 (revision 62889) Property changes on: ruby_2_5 ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /trunk:r62042,62044 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/