summaryrefslogtreecommitdiff
path: root/hash.c
diff options
context:
space:
mode:
authorKenichi Kamiya <kachick1@gmail.com>2021-03-28 09:14:57 +0900
committerGitHub <noreply@github.com>2021-03-28 09:14:57 +0900
commit31e0382723bfb35cffe3ca485dd0577668cafa07 (patch)
tree09d09fa724f0a0ea6f47b7d13328c00c18b5bc97 /hash.c
parente398a0e53a7207152fb2139f1e4485968a07f9de (diff)
Keep non evaluated keys in `Hash#transform_keys!` [Bug #17735]
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/4294 Merged-By: nobu <nobu@ruby-lang.org>
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c6
1 files changed, 5 insertions, 1 deletions
diff --git a/hash.c b/hash.c
index fdecb1edad..d6d3752fb7 100644
--- a/hash.c
+++ b/hash.c
@@ -3268,8 +3268,8 @@ rb_hash_transform_keys_bang(int argc, VALUE *argv, VALUE hash)
rb_hash_modify_check(hash);
if (!RHASH_TABLE_EMPTY_P(hash)) {
long i;
+ VALUE new_keys = hash_alloc(0);
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, val;
@@ -3286,7 +3286,11 @@ rb_hash_transform_keys_bang(int argc, VALUE *argv, VALUE hash)
new_key = key;
}
val = RARRAY_AREF(pairs, i+1);
+ if (!hash_stlike_lookup(new_keys, key, NULL)) {
+ rb_hash_stlike_delete(hash, &key, NULL);
+ }
rb_hash_aset(hash, new_key, val);
+ rb_hash_aset(new_keys, new_key, Qnil);
}
}
return hash;