summaryrefslogtreecommitdiff
path: root/hash.c
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2022-11-09 09:59:25 -0800
committerJeremy Evans <code@jeremyevans.net>2023-03-24 10:55:13 -0700
commit5d6579bd9129cfbd62702fb42b249338807a34a2 (patch)
tree0b044ca2faae821f4cd465e65e696ffc130fb131 /hash.c
parent1b13db25d8ed0f328acc4e86668f8ad7deae7221 (diff)
downloadruby-5d6579bd9129cfbd62702fb42b249338807a34a2.tar.gz
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]
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c16
1 files changed, 5 insertions, 11 deletions
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;
}