Skip to content
Browse files

Speed up some Hash core extensions.

  • Loading branch information...
1 parent 328b0b1 commit c0262827cacc1baf16668af65c35a09138166394 José Valim and Mikel Lindsaar committed Jan 24, 2010
View
13 activesupport/lib/active_support/core_ext/hash/deep_merge.rb
@@ -1,17 +1,16 @@
class Hash
# Returns a new hash with +self+ and +other_hash+ merged recursively.
def deep_merge(other_hash)
- target = dup
- other_hash.each_pair do |k,v|
- tv = target[k]
- target[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_merge(v) : v
- end
- target
+ dup.deep_merge!(other_hash)

Just wondering: shouldn't this be deep_dup.deep_merge!?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
end
# Returns a new hash with +self+ and +other_hash+ merged recursively.
# Modifies the receiver in place.
def deep_merge!(other_hash)
- replace(deep_merge(other_hash))
+ other_hash.each_pair do |k,v|
+ tv = self[k]
+ self[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_merge(v) : v
+ end
+ self
end
end
View
15 activesupport/lib/active_support/core_ext/hash/keys.rb
@@ -1,10 +1,7 @@
class Hash
# Return a new hash with all keys converted to strings.
def stringify_keys
- inject({}) do |options, (key, value)|
- options[key.to_s] = value
- options
- end
+ dup.stringify_keys!
end
# Destructively convert all keys to strings.
@@ -18,16 +15,16 @@ def stringify_keys!
# Return a new hash with all keys converted to symbols, as long as
# they respond to +to_sym+.
def symbolize_keys
- inject({}) do |options, (key, value)|
- options[(key.to_sym rescue key) || key] = value
- options
- end
+ dup.symbolize_keys!
end
# Destructively convert all keys to symbols, as long as they respond
# to +to_sym+.
def symbolize_keys!
- self.replace(self.symbolize_keys)
+ keys.each do |key|
+ self[(key.to_sym rescue key) || key] = delete(key)
+ end
+ self
end
alias_method :to_options, :symbolize_keys

0 comments on commit c026282

Please sign in to comment.
Something went wrong with that request. Please try again.