aboutsummaryrefslogtreecommitdiffstats
path: root/hash.c
diff options
context:
space:
mode:
authormrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-01-26 01:33:45 +0000
committermrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-01-26 01:33:45 +0000
commitc32f43aeae2885593c16b42a150e1a160ec42154 (patch)
treec2d354325f138d075e6847cc7bb639b131e5af24 /hash.c
parentdf7bc5bd0e7041a29d656fa3a57b433f2caaf825 (diff)
downloadruby-c32f43aeae2885593c16b42a150e1a160ec42154.tar.gz
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 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62042 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c16
1 files changed, 10 insertions, 6 deletions
diff --git a/hash.c b/hash.c
index c36b865869..43db7951ee 100644
--- a/hash.c
+++ b/hash.c
@@ -1917,6 +1917,8 @@ rb_hash_transform_keys(VALUE hash)
return result;
}
+static VALUE rb_hash_flatten(int argc, VALUE *argv, VALUE hash);
+
/*
* call-seq:
* hsh.transform_keys! {|key| block } -> hsh
@@ -1940,12 +1942,14 @@ rb_hash_transform_keys_bang(VALUE hash)
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;
}