Skip to content

Commit bf5dbfc

Browse files
committed
Fix HWIA#transform_keys! removing defaults
A [previous change][1] fixed `HWIA#transform_keys!` from clobbering keys if they ended up transforming into another pre-transformed key. However, that change also resulted in `HWIA#transform_keys!` clearing the `default`/`default_proc`. This commit fixes that issue and adds test coverage to both `#transform_keys` and `#transform_keys!` to ensure that their behavior with regard to `default`/`default_proc` is consistent with `Hash`. ```irb irb(main):001> Hash.new(:a).transform_keys(&:to_s).default => nil irb(main):002> Hash.new(:a).transform_keys!(&:to_s).default => :a ``` [1]: f49d7a0
1 parent 40a3f2f commit bf5dbfc

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

activesupport/lib/active_support/hash_with_indifferent_access.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,12 +368,12 @@ def transform_keys(hash = NOT_GIVEN, &block)
368368
def transform_keys!(hash = NOT_GIVEN, &block)
369369
if NOT_GIVEN.equal?(hash)
370370
if block_given?
371-
replace(transform_keys(&block))
371+
replace(transform_keys(&block).tap { |h| set_defaults(h) })
372372
else
373373
return to_enum(:transform_keys!)
374374
end
375375
else
376-
replace(transform_keys(hash, &block))
376+
replace(transform_keys(hash, &block).tap { |h| set_defaults(h) })
377377
end
378378

379379
self

activesupport/test/hash_with_indifferent_access_test.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,18 @@ def test_indifferent_transform_keys
486486
assert_raise TypeError do
487487
hash.transform_keys(nil)
488488
end
489+
490+
hash_with_default = Hash.new(:a)
491+
hash = ActiveSupport::HashWithIndifferentAccess.new(hash_with_default).transform_keys(&:to_s)
492+
assert_nil hash.default
493+
hash = ActiveSupport::HashWithIndifferentAccess.new(hash_with_default).transform_keys { |k| k.to_s }
494+
assert_nil hash.default
495+
496+
hash_with_default_proc = Hash.new { |h, k| h[k] = :b }
497+
hash = ActiveSupport::HashWithIndifferentAccess.new(hash_with_default_proc).transform_keys(&:to_s)
498+
assert_nil hash.default_proc
499+
hash = ActiveSupport::HashWithIndifferentAccess.new(hash_with_default_proc).transform_keys { |k| k.to_s }
500+
assert_nil hash.default_proc
489501
end
490502

491503
def test_indifferent_deep_transform_keys
@@ -540,6 +552,18 @@ def test_indifferent_transform_keys_bang
540552
assert_raise TypeError do
541553
hash.transform_keys(nil)
542554
end
555+
556+
hash_with_default = Hash.new(:a)
557+
hash = ActiveSupport::HashWithIndifferentAccess.new(hash_with_default).transform_keys!(&:to_s)
558+
assert_equal hash_with_default.default, hash.default
559+
hash = ActiveSupport::HashWithIndifferentAccess.new(hash_with_default).transform_keys! { |k| k.to_s }
560+
assert_equal hash_with_default.default, hash.default
561+
562+
hash_with_default_proc = Hash.new { |h, k| h[k] = :b }
563+
hash = ActiveSupport::HashWithIndifferentAccess.new(hash_with_default_proc).transform_keys!(&:to_s)
564+
assert_equal hash_with_default_proc.default_proc, hash.default_proc
565+
hash = ActiveSupport::HashWithIndifferentAccess.new(hash_with_default_proc).transform_keys! { |k| k.to_s }
566+
assert_equal hash_with_default_proc.default_proc, hash.default_proc
543567
end
544568

545569
def test_indifferent_deep_transform_keys_bang

0 commit comments

Comments
 (0)