Skip to content

Commit

Permalink
add examples to Hash#deep_stringify_keys and Hash#deep_symbolize_keys…
Browse files Browse the repository at this point in the history
… [ci skip]
  • Loading branch information
Francesco Rodriguez committed May 27, 2012
1 parent 7d8474e commit ac50b63
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions activesupport/lib/active_support/core_ext/hash/keys.rb
Expand Up @@ -93,6 +93,11 @@ def deep_transform_keys!(&block)
# Return a new hash with all keys converted to strings.
# This includes the keys from the root hash and from all
# nested hashes.
#
# hash = { person: { name: 'Rob', age: '28' } }
#
# hash.deep_stringify_keys
# # => { "person" => { "name" => "Rob", "age" => "28" } }
def deep_stringify_keys
deep_transform_keys{ |key| key.to_s }
end
Expand All @@ -107,6 +112,11 @@ def deep_stringify_keys!
# 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.
#
# hash = { 'person' => { 'name' => 'Rob', 'age' => '28' } }
#
# hash.deep_symbolize_keys
# # => { person: { name: "Rob", age: "28" } }
def deep_symbolize_keys
deep_transform_keys{ |key| key.to_sym rescue key }
end
Expand Down

0 comments on commit ac50b63

Please sign in to comment.