From 5d6579bd9129cfbd62702fb42b249338807a34a2 Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Wed, 9 Nov 2022 09:59:25 -0800 Subject: Change Hash#compact to keep default values and compare_by_identity flag The documentation states it returns a copy of self with nil value entries removed. However, the previous behavior was creating a plain new hash with non-nil values copied into it. This change aligns the behavior with the documentation. Fixes [Bug #19113] --- hash.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) (limited to 'hash.c') diff --git a/hash.c b/hash.c index 285a13494a..3d58bd9770 100644 --- a/hash.c +++ b/hash.c @@ -4307,15 +4307,6 @@ delete_if_nil(VALUE key, VALUE value, VALUE hash) return ST_CONTINUE; } -static int -set_if_not_nil(VALUE key, VALUE value, VALUE hash) -{ - if (!NIL_P(value)) { - rb_hash_aset(hash, key, value); - } - return ST_CONTINUE; -} - /* * call-seq: * hash.compact -> new_hash @@ -4329,9 +4320,12 @@ set_if_not_nil(VALUE key, VALUE value, VALUE hash) static VALUE rb_hash_compact(VALUE hash) { - VALUE result = rb_hash_new(); + VALUE result = rb_hash_dup(hash); if (!RHASH_EMPTY_P(hash)) { - rb_hash_foreach(hash, set_if_not_nil, result); + rb_hash_foreach(result, delete_if_nil, result); + } + else if (rb_hash_compare_by_id_p(hash)) { + result = rb_hash_compare_by_id(result); } return result; } -- cgit v1.2.1