Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/active_support/core_ext/hash/hash.rb
Instance Method Summary collapse
-
#symbolize_keys ⇒ Object
hash = { ‘name’ => ‘Rob’, ‘age’ => ‘28’ } hash.symbolize_keys => { name: “Rob”, age: “28” }.
-
#symbolize_keys! ⇒ Object
File activesupport/lib/active_support/core_ext/hash/keys.rb, line 135.
-
#transform_keys ⇒ Object
File activesupport/lib/active_support/core_ext/hash/keys.rb.
- #transform_keys!(&block) ⇒ Object
Instance Method Details
#symbolize_keys ⇒ Object
hash = { ‘name’ => ‘Rob’, ‘age’ => ‘28’ } hash.symbolize_keys
> { name: “Rob”, age: “28” }
27 28 29 |
# File 'lib/active_support/core_ext/hash/hash.rb', line 27 def symbolize_keys transform_keys{ |key| key.to_sym rescue key } end |
#symbolize_keys! ⇒ Object
File activesupport/lib/active_support/core_ext/hash/keys.rb, line 135
32 33 34 |
# File 'lib/active_support/core_ext/hash/hash.rb', line 32 def symbolize_keys! transform_keys!{ |key| key.to_sym rescue key } end |
#transform_keys ⇒ Object
File activesupport/lib/active_support/core_ext/hash/keys.rb
hash = { name: ‘Rob’, age: ‘28’ } hash.transform_keys{ |key| key.to_s.upcase }
> { “NAME” => “Rob”, “AGE” => “28” }
8 9 10 11 12 13 14 |
# File 'lib/active_support/core_ext/hash/hash.rb', line 8 def transform_keys result = {} each_key do |key| result[yield(key)] = self[key] end result end |
#transform_keys!(&block) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/active_support/core_ext/hash/hash.rb', line 16 def transform_keys!(&block) keys.each do |key| value = delete(key) self[yield(key)] = value.is_a?(Hash) ? value.transform_keys!(&block) : value end self end |