Skip to content

Commit

Permalink
ActiveSupport Hash optimizations [#2902 state:resolved]
Browse files Browse the repository at this point in the history
Signed-off-by: Carl Lerche <carllerche@mac.com>
  • Loading branch information
methodmissing authored and Carl Lerche committed Jul 15, 2009
1 parent 40b3875 commit 0920e69
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
Expand Up @@ -4,7 +4,7 @@ def deep_merge(other_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

Expand Down
2 changes: 1 addition & 1 deletion activesupport/lib/active_support/core_ext/hash/diff.rb
Expand Up @@ -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
Expand Up @@ -21,7 +21,7 @@ def reverse_merge(other_hash)
# Performs the opposite of <tt>merge</tt>, 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!
Expand Down
Expand Up @@ -98,6 +98,10 @@ def reverse_merge(other_hash)
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))
Expand All @@ -109,7 +113,7 @@ def to_options!; self end

# Convert to a Hash with String keys.
def to_hash
Hash.new(default).merge(self)
Hash.new(default).merge!(self)
end

protected
Expand Down

0 comments on commit 0920e69

Please sign in to comment.