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] --- spec/ruby/core/hash/compact_spec.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'spec/ruby') diff --git a/spec/ruby/core/hash/compact_spec.rb b/spec/ruby/core/hash/compact_spec.rb index 2989afc8b7..76aa43949d 100644 --- a/spec/ruby/core/hash/compact_spec.rb +++ b/spec/ruby/core/hash/compact_spec.rb @@ -18,6 +18,30 @@ describe "Hash#compact" do @hash.compact @hash.should == @initial_pairs end + + ruby_version_is '3.3' do + it "retains the default value" do + hash = Hash.new(1) + hash.compact.default.should == 1 + hash[:a] = 1 + hash.compact.default.should == 1 + end + + it "retains the default_proc" do + pr = proc { |h, k| h[k] = [] } + hash = Hash.new(&pr) + hash.compact.default_proc.should == pr + hash[:a] = 1 + hash.compact.default_proc.should == pr + end + + it "retains compare_by_identity_flag" do + hash = {}.compare_by_identity + hash.compact.compare_by_identity?.should == true + hash[:a] = 1 + hash.compact.compare_by_identity?.should == true + end + end end describe "Hash#compact!" do -- cgit v1.2.3