mirror of https://github.com/rails/rails
Speed up some Hash core extensions.
This commit is contained in:
parent
328b0b1268
commit
c0262827ca
|
@ -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)
|
||||
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
|
||||
|
|
|
@ -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 @@ class Hash
|
|||
# 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
|
||||
|
|
Loading…
Reference in New Issue