diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-05-23 11:41:58 -0700 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-05-23 11:41:58 -0700 |
commit | 7a23ada0e5b30e4b511be7a39385121255503e2a (patch) | |
tree | cc015acd11a3ab3d13748d79aef115e1bc7b0ce8 /activesupport/lib/active_support/core_ext/hash/keys.rb | |
parent | 82b05fbcf4869517c08fb62398b03f3f133c0a28 (diff) | |
parent | 2b5b60fc3c4251536b837937391ebdd028892b15 (diff) | |
download | rails-7a23ada0e5b30e4b511be7a39385121255503e2a.tar.gz rails-7a23ada0e5b30e4b511be7a39385121255503e2a.tar.bz2 rails-7a23ada0e5b30e4b511be7a39385121255503e2a.zip |
Merge pull request #6466 from markmcspadden/hash_deep_transform_keys
Add Hash#deep_transform_keys and Hash#deep_transform_keys! Also convert ...
Diffstat (limited to 'activesupport/lib/active_support/core_ext/hash/keys.rb')
-rw-r--r-- | activesupport/lib/active_support/core_ext/hash/keys.rb | 43 |
1 files changed, 26 insertions, 17 deletions
diff --git a/activesupport/lib/active_support/core_ext/hash/keys.rb b/activesupport/lib/active_support/core_ext/hash/keys.rb index 5eb861934d..362d584ba1 100644 --- a/activesupport/lib/active_support/core_ext/hash/keys.rb +++ b/activesupport/lib/active_support/core_ext/hash/keys.rb @@ -65,47 +65,56 @@ class Hash end end - # Return a new hash with all keys converted to strings. + # Return a new hash with all keys converted by the block operation. # This includes the keys from the root hash and from all # nested hashes. - def deep_stringify_keys + # + # { :person => { :name => 'Rob', :years => '28' } }.deep_transform_keys{ |key| key.to_s.upcase } + # # => { "PERSON" => { "NAME" => "Rob", "YEARS" => "28" } } + def deep_transform_keys(&block) result = {} each do |key, value| - result[key.to_s] = value.is_a?(Hash) ? value.deep_stringify_keys : value + result[yield(key)] = value.is_a?(Hash) ? value.deep_transform_keys(&block) : value end result end - # Destructively convert all keys to strings. + # Destructively convert all keys by using the block operation. # This includes the keys from the root hash and from all # nested hashes. - def deep_stringify_keys! + def deep_transform_keys!(&block) keys.each do |key| - val = delete(key) - self[key.to_s] = val.is_a?(Hash) ? val.deep_stringify_keys! : val + value = delete(key) + self[yield(key)] = value.is_a?(Hash) ? value.deep_transform_keys!(&block) : value end self end + # Return a new hash with all keys converted to strings. + # This includes the keys from the root hash and from all + # nested hashes. + def deep_stringify_keys + deep_transform_keys{ |key| key.to_s } + end + + # Destructively convert all keys to strings. + # This includes the keys from the root hash and from all + # nested hashes. + def deep_stringify_keys! + deep_transform_keys!{ |key| key.to_s } + end + # Destructively convert all keys to symbols, as long as they respond # to +to_sym+. This includes the keys from the root hash and from all # nested hashes. def deep_symbolize_keys! - keys.each do |key| - val = delete(key) - self[(key.to_sym rescue key)] = val.is_a?(Hash) ? val.deep_stringify_keys! : val - end - self + deep_transform_keys!{ |key| key.to_sym rescue key } end # Return a new hash with all keys converted to symbols, as long as # they respond to +to_sym+. This includes the keys from the root hash # and from all nested hashes. def deep_symbolize_keys - result = {} - each do |key, value| - result[(key.to_sym rescue key)] = value.is_a?(Hash) ? value.deep_symbolize_keys : value - end - result + deep_transform_keys{ |key| key.to_sym rescue key } end end |