Skip to content

Commit 98c6e4e

Browse files
authored
Merge pull request rails#27392 from y-yagi/use_same_class_on_compact
ensure `#compact` of HWIDA to return HWIDA
2 parents e482dce + d97ba34 commit 98c6e4e

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

activesupport/lib/active_support/hash_with_indifferent_access.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@ def transform_values(*args, &block)
269269
dup.tap { |hash| hash.transform_values!(*args, &block) }
270270
end
271271

272+
def compact
273+
dup.compact!
274+
end
275+
272276
# Convert to a regular hash with string keys.
273277
def to_hash
274278
_new_hash = Hash.new

activesupport/test/core_ext/hash_ext_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,16 @@ def test_indifferent_reject_bang
589589
assert_instance_of ActiveSupport::HashWithIndifferentAccess, indifferent_strings
590590
end
591591

592+
def test_indifferent_compact
593+
hash_contain_nil_value = @strings.merge("z" => nil)
594+
hash = ActiveSupport::HashWithIndifferentAccess.new(hash_contain_nil_value)
595+
compacted_hash = hash.compact
596+
597+
assert_equal(@strings, compacted_hash)
598+
assert_equal(hash_contain_nil_value, hash)
599+
assert_instance_of ActiveSupport::HashWithIndifferentAccess, compacted_hash
600+
end
601+
592602
def test_indifferent_to_hash
593603
# Should convert to a Hash with String keys.
594604
assert_equal @strings, @mixed.with_indifferent_access.to_hash

0 commit comments

Comments
 (0)