Skip to content

Commit ce0046f

Browse files
committed
Refactor HashWithIndifferentAccess not to rely on tap as much
1 parent bf5dbfc commit ce0046f

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
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).tap { |h| set_defaults(h) })
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).tap { |h| set_defaults(h) })
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: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -555,15 +555,20 @@ def test_indifferent_transform_keys_bang
555555

556556
hash_with_default = Hash.new(:a)
557557
hash = ActiveSupport::HashWithIndifferentAccess.new(hash_with_default).transform_keys!(&:to_s)
558-
assert_equal hash_with_default.default, hash.default
558+
assert_equal :a, hash.default
559+
assert_equal :a, hash_with_default.default
560+
559561
hash = ActiveSupport::HashWithIndifferentAccess.new(hash_with_default).transform_keys! { |k| k.to_s }
560-
assert_equal hash_with_default.default, hash.default
562+
assert_equal :a, hash.default
563+
assert_equal :a, hash_with_default.default
561564

562565
hash_with_default_proc = Hash.new { |h, k| h[k] = :b }
566+
default_proc = hash_with_default_proc.default_proc
567+
563568
hash = ActiveSupport::HashWithIndifferentAccess.new(hash_with_default_proc).transform_keys!(&:to_s)
564-
assert_equal hash_with_default_proc.default_proc, hash.default_proc
569+
assert_equal default_proc, hash.default_proc
565570
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
571+
assert_equal default_proc, hash.default_proc
567572
end
568573

569574
def test_indifferent_deep_transform_keys_bang

0 commit comments

Comments
 (0)