Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/hash-ext.rb
Overview
Copied from ActiveSupport, included if we don’t already have it
Instance Method Summary collapse
-
#stringify_keys ⇒ Object
Return a new hash with all keys converted to strings.
- #stringify_keys! ⇒ Object
-
#symbolize_keys ⇒ Object
Return a new hash with all keys converted to symbols.
- #symbolize_keys! ⇒ Object
Instance Method Details
#stringify_keys ⇒ Object
Return a new hash with all keys converted to strings.
15 16 17 18 19 20 |
# File 'lib/hash-ext.rb', line 15 def stringify_keys inject({}) do |, (key, value)| [(key.to_s rescue key) || key] = value end end |
#stringify_keys! ⇒ Object
21 22 23 |
# File 'lib/hash-ext.rb', line 21 def stringify_keys! self.replace(self.stringify_keys) end |
#symbolize_keys ⇒ Object
Return a new hash with all keys converted to symbols.
5 6 7 8 9 10 |
# File 'lib/hash-ext.rb', line 5 def symbolize_keys inject({}) do |, (key, value)| [(key.downcase.to_sym rescue key) || key] = value end end |
#symbolize_keys! ⇒ Object
11 12 13 |
# File 'lib/hash-ext.rb', line 11 def symbolize_keys! self.replace(self.symbolize_keys) end |