From 4b411440eac6b316667f934182bb5c85fb80d126 Mon Sep 17 00:00:00 2001 From: Lourens Naude Date: Sun, 12 Jul 2009 12:18:04 +0100 Subject: [PATCH] ActiveSupport Hash optimizations --- .../lib/active_support/core_ext/hash/deep_merge.rb | 2 +- .../lib/active_support/core_ext/hash/diff.rb | 2 +- .../active_support/core_ext/hash/reverse_merge.rb | 2 +- .../active_support/hash_with_indifferent_access.rb | 6 +++++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/hash/deep_merge.rb b/activesupport/lib/active_support/core_ext/hash/deep_merge.rb index b009be3..ffde34a 100644 --- a/activesupport/lib/active_support/core_ext/hash/deep_merge.rb +++ b/activesupport/lib/active_support/core_ext/hash/deep_merge.rb @@ -4,7 +4,7 @@ class Hash merge(other_hash) do |key, oldval, newval| oldval = oldval.to_hash if oldval.respond_to?(:to_hash) newval = newval.to_hash if newval.respond_to?(:to_hash) - oldval.class.to_s == 'Hash' && newval.class.to_s == 'Hash' ? oldval.deep_merge(newval) : newval + oldval.is_a?( Hash ) && newval.is_a?( Hash ) ? oldval.deep_merge(newval) : newval end end diff --git a/activesupport/lib/active_support/core_ext/hash/diff.rb b/activesupport/lib/active_support/core_ext/hash/diff.rb index da98593..b904f49 100644 --- a/activesupport/lib/active_support/core_ext/hash/diff.rb +++ b/activesupport/lib/active_support/core_ext/hash/diff.rb @@ -8,6 +8,6 @@ class Hash # {}.diff(1 => 2) # => {1 => 2} # {1 => 2, 3 => 4}.diff(1 => 2) # => {3 => 4} def diff(h2) - dup.delete_if { |k, v| h2[k] == v }.merge(h2.dup.delete_if { |k, v| has_key?(k) }) + dup.delete_if { |k, v| h2[k] == v }.merge!(h2.dup.delete_if { |k, v| has_key?(k) }) end end diff --git a/activesupport/lib/active_support/core_ext/hash/reverse_merge.rb b/activesupport/lib/active_support/core_ext/hash/reverse_merge.rb index ebfdcb2..d7ebd5f 100644 --- a/activesupport/lib/active_support/core_ext/hash/reverse_merge.rb +++ b/activesupport/lib/active_support/core_ext/hash/reverse_merge.rb @@ -21,7 +21,7 @@ class Hash # Performs the opposite of merge, with the keys and values from the first hash taking precedence over the second. # Modifies the receiver in place. def reverse_merge!(other_hash) - replace(reverse_merge(other_hash)) + merge!( other_hash ){|k,o,n| o } end alias_method :reverse_update, :reverse_merge! diff --git a/activesupport/lib/active_support/hash_with_indifferent_access.rb b/activesupport/lib/active_support/hash_with_indifferent_access.rb index 61fc647..543dab4 100644 --- a/activesupport/lib/active_support/hash_with_indifferent_access.rb +++ b/activesupport/lib/active_support/hash_with_indifferent_access.rb @@ -98,6 +98,10 @@ module ActiveSupport super other_hash.with_indifferent_access end + def reverse_merge!(other_hash) + replace(reverse_merge( other_hash )) + end + # Removes a specified key from the hash. def delete(key) super(convert_key(key)) @@ -109,7 +113,7 @@ module ActiveSupport # Convert to a Hash with String keys. def to_hash - Hash.new(default).merge(self) + Hash.new(default).merge!(self) end protected -- 1.5.4.5