Skip to content

Commit 29713d7

Browse files
author
Francesco Rodriguez
committed
update Hash documentation with 1.9 syntax [ci skip]
1 parent ac50b63 commit 29713d7

File tree

1 file changed

+16
-8
lines changed
  • activesupport/lib/active_support/core_ext/hash

1 file changed

+16
-8
lines changed

activesupport/lib/active_support/core_ext/hash/keys.rb

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
class Hash
22
# Return a new hash with all keys converted using the block operation.
33
#
4-
# { :name => 'Rob', :years => '28' }.transform_keys{ |key| key.to_s.upcase }
5-
# # => { "NAME" => "Rob", "YEARS" => "28" }
4+
# hash = { name: 'Rob', age: '28' }
5+
#
6+
# hash.transform_keys{ |key| key.to_s.upcase }
7+
# # => { "NAME" => "Rob", "AGE" => "28" }
68
def transform_keys
79
result = {}
810
keys.each do |key|
@@ -22,8 +24,10 @@ def transform_keys!
2224

2325
# Return a new hash with all keys converted to strings.
2426
#
25-
# { :name => 'Rob', :years => '28' }.stringify_keys
26-
# #=> { "name" => "Rob", "years" => "28" }
27+
# hash = { name: 'Rob', age: '28' }
28+
#
29+
# hash.stringify_keys
30+
# #=> { "name" => "Rob", "age" => "28" }
2731
def stringify_keys
2832
transform_keys{ |key| key.to_s }
2933
end
@@ -37,8 +41,10 @@ def stringify_keys!
3741
# Return a new hash with all keys converted to symbols, as long as
3842
# they respond to +to_sym+.
3943
#
40-
# { 'name' => 'Rob', 'years' => '28' }.symbolize_keys
41-
# #=> { :name => "Rob", :years => "28" }
44+
# hash = { 'name' => 'Rob', 'age' => '28' }
45+
#
46+
# hash.symbolize_keys
47+
# #=> { name: "Rob", age: "28" }
4248
def symbolize_keys
4349
transform_keys{ |key| key.to_sym rescue key }
4450
end
@@ -69,8 +75,10 @@ def assert_valid_keys(*valid_keys)
6975
# This includes the keys from the root hash and from all
7076
# nested hashes.
7177
#
72-
# { :person => { :name => 'Rob', :years => '28' } }.deep_transform_keys{ |key| key.to_s.upcase }
73-
# # => { "PERSON" => { "NAME" => "Rob", "YEARS" => "28" } }
78+
# hash = { person: { name: 'Rob', age: '28' } }
79+
#
80+
# hash.deep_transform_keys{ |key| key.to_s.upcase }
81+
# # => { "PERSON" => { "NAME" => "Rob", "AGE" => "28" } }
7482
def deep_transform_keys(&block)
7583
result = {}
7684
each do |key, value|

0 commit comments

Comments
 (0)