Skip to content

Commit ed1e55b

Browse files
authored
Merge pull request rails#55419 from skipkayhil/hm-klrrkwllnnzrmltn
Fix `HWIA#transform_keys!` removing defaults
2 parents 40a3f2f + ce0046f commit ed1e55b

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

activesupport/lib/active_support/hash_with_indifferent_access.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,7 @@ def fetch_values(*indices, &block)
278278
# hash[:a][:c] # => "c"
279279
# dup[:a][:c] # => "c"
280280
def dup
281-
self.class.new(self).tap do |new_hash|
282-
set_defaults(new_hash)
283-
end
281+
copy_defaults(self.class.new(self))
284282
end
285283

286284
# This method has the same semantics of +update+, except it does not
@@ -368,12 +366,12 @@ def transform_keys(hash = NOT_GIVEN, &block)
368366
def transform_keys!(hash = NOT_GIVEN, &block)
369367
if NOT_GIVEN.equal?(hash)
370368
if block_given?
371-
replace(transform_keys(&block))
369+
replace(copy_defaults(transform_keys(&block)))
372370
else
373371
return to_enum(:transform_keys!)
374372
end
375373
else
376-
replace(transform_keys(hash, &block))
374+
replace(copy_defaults(transform_keys(hash, &block)))
377375
end
378376

379377
self
@@ -397,8 +395,7 @@ def compact
397395
def to_hash
398396
copy = Hash[self]
399397
copy.transform_values! { |v| convert_value_to_hash(v) }
400-
set_defaults(copy)
401-
copy
398+
copy_defaults(copy)
402399
end
403400

404401
def to_proc
@@ -438,12 +435,13 @@ def convert_value_to_hash(value)
438435
end
439436

440437

441-
def set_defaults(target)
438+
def copy_defaults(target)
442439
if default_proc
443440
target.default_proc = default_proc.dup
444441
else
445442
target.default = default
446443
end
444+
target
447445
end
448446

449447
def update_with_single_argument(other_hash, block)

activesupport/test/hash_with_indifferent_access_test.rb

Lines changed: 29 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,23 @@ 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 :a, hash.default
559+
assert_equal :a, hash_with_default.default
560+
561+
hash = ActiveSupport::HashWithIndifferentAccess.new(hash_with_default).transform_keys! { |k| k.to_s }
562+
assert_equal :a, hash.default
563+
assert_equal :a, hash_with_default.default
564+
565+
hash_with_default_proc = Hash.new { |h, k| h[k] = :b }
566+
default_proc = hash_with_default_proc.default_proc
567+
568+
hash = ActiveSupport::HashWithIndifferentAccess.new(hash_with_default_proc).transform_keys!(&:to_s)
569+
assert_equal default_proc, hash.default_proc
570+
hash = ActiveSupport::HashWithIndifferentAccess.new(hash_with_default_proc).transform_keys! { |k| k.to_s }
571+
assert_equal default_proc, hash.default_proc
543572
end
544573

545574
def test_indifferent_deep_transform_keys_bang

0 commit comments

Comments
 (0)