aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activesupport/lib/active_support/core_ext/hash/deep_merge.rb13
-rw-r--r--activesupport/lib/active_support/core_ext/hash/keys.rb15
2 files changed, 12 insertions, 16 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 24d0a2a481..af771c86ff 100644
--- a/activesupport/lib/active_support/core_ext/hash/deep_merge.rb
+++ b/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)
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
diff --git a/activesupport/lib/active_support/core_ext/hash/keys.rb b/activesupport/lib/active_support/core_ext/hash/keys.rb
index ecd63293b4..045a6944fa 100644
--- a/activesupport/lib/active_support/core_ext/hash/keys.rb
+++ b/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 @@ 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