Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/core_extensions.rb
Instance Method Summary collapse
- #stringify_keys ⇒ Object
-
#stringify_keys! ⇒ Object
Destructively convert all keys to strings.
-
#symbolize_keys ⇒ Object
Return a new hash with all keys converted to symbols.
-
#symbolize_keys! ⇒ Object
Destructively convert all keys to symbols.
Instance Method Details
#stringify_keys ⇒ Object
2 3 4 5 6 7 |
# File 'lib/core_extensions.rb', line 2 def stringify_keys inject({}) do |, (key, value)| [key.to_s] = value end end |
#stringify_keys! ⇒ Object
Destructively convert all keys to strings.
10 11 12 13 14 15 |
# File 'lib/core_extensions.rb', line 10 def stringify_keys! keys.each do |key| self[key.to_s] = delete(key) end self end |
#symbolize_keys ⇒ Object
Return a new hash with all keys converted to symbols.
18 19 20 21 22 23 |
# File 'lib/core_extensions.rb', line 18 def symbolize_keys inject({}) do |, (key, value)| [(key.to_sym rescue key) || key] = value end end |
#symbolize_keys! ⇒ Object
Destructively convert all keys to symbols.
26 27 28 |
# File 'lib/core_extensions.rb', line 26 def symbolize_keys! self.replace(self.symbolize_keys) end |