Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/core_ext/hash_ext.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#stringify_keys ⇒ Object
Return a new hash with all keys converted to strings.
-
#stringify_keys! ⇒ Object
Destructively convert all keys to strings.
-
#symbolize_keys ⇒ Object
(also: #to_options)
Return a new hash with all keys converted to symbols.
-
#symbolize_keys! ⇒ Object
(also: #to_options!)
Destructively convert all keys to symbols.
Instance Method Details
#stringify_keys ⇒ Object
Return a new hash with all keys converted to strings.
4 5 6 7 8 9 |
# File 'lib/core_ext/hash_ext.rb', line 4 def stringify_keys inject({}) do |, (key, value)| [key.to_s] = value end end |
#stringify_keys! ⇒ Object
Destructively convert all keys to strings.
12 13 14 15 16 17 |
# File 'lib/core_ext/hash_ext.rb', line 12 def stringify_keys! keys.each do |key| self[key.to_s] = delete(key) end self end |
#symbolize_keys ⇒ Object Also known as: to_options
Return a new hash with all keys converted to symbols.
20 21 22 23 24 25 |
# File 'lib/core_ext/hash_ext.rb', line 20 def symbolize_keys inject({}) do |, (key, value)| [(key.to_sym rescue key) || key] = value end end |
#symbolize_keys! ⇒ Object Also known as: to_options!
Destructively convert all keys to symbols.
28 29 30 |
# File 'lib/core_ext/hash_ext.rb', line 28 def symbolize_keys! self.replace(self.symbolize_keys) end |