Skip to content
Browse files

add examples to Hash#deep_stringify_keys and Hash#deep_symbolize_keys…

… [ci skip]
  • Loading branch information...
1 parent 7d8474e commit ac50b633d4b9fd12bf0212a5cf8ebc5ab3ee8f37 Francesco Rodriguez committed
Showing with 10 additions and 0 deletions.
  1. +10 −0 activesupport/lib/active_support/core_ext/hash/keys.rb
View
10 activesupport/lib/active_support/core_ext/hash/keys.rb
@@ -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
@@ -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

0 comments on commit ac50b63

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