Skip to content

Commit 6e95f09

Browse files
committed
Avoid some useless copies in HashWithIndifferentAccess
1 parent f49d7a0 commit 6e95f09

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

activesupport/lib/active_support/hash_with_indifferent_access.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,13 +297,13 @@ def merge(*hashes, &block)
297297
# hash['a'] = nil
298298
# hash.reverse_merge(a: 0, b: 1) # => {"a"=>nil, "b"=>1}
299299
def reverse_merge(other_hash)
300-
super(self.class.new(other_hash))
300+
super(cast(other_hash))
301301
end
302302
alias_method :with_defaults, :reverse_merge
303303

304304
# Same semantics as +reverse_merge+ but modifies the receiver in-place.
305305
def reverse_merge!(other_hash)
306-
super(self.class.new(other_hash))
306+
super(cast(other_hash))
307307
end
308308
alias_method :with_defaults!, :reverse_merge!
309309

@@ -312,7 +312,7 @@ def reverse_merge!(other_hash)
312312
# h = { "a" => 100, "b" => 200 }
313313
# h.replace({ "c" => 300, "d" => 400 }) # => {"c"=>300, "d"=>400}
314314
def replace(other_hash)
315-
super(self.class.new(other_hash))
315+
super(cast(other_hash))
316316
end
317317

318318
# Removes the specified key from the hash.
@@ -406,6 +406,10 @@ def to_proc
406406
end
407407

408408
private
409+
def cast(other)
410+
self.class === other ? other : self.class.new(other)
411+
end
412+
409413
def convert_key(key)
410414
Symbol === key ? key.name : key
411415
end

0 commit comments

Comments
 (0)