Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/howzit/hash.rb
Overview
Hash helpers
Instance Method Summary collapse
-
#deep_freeze ⇒ Hash
Freeze all values in a hash.
-
#deep_freeze! ⇒ Object
Deep freeze a hash in place (destructive).
-
#deep_thaw ⇒ Hash
Unfreeze nested hash values.
-
#deep_thaw! ⇒ Object
Unfreeze nested hash values in place (destructive).
-
#stringify_keys ⇒ Hash
Turn all keys into string.
-
#stringify_keys! ⇒ Object
Turn all keys into strings in place (destructive).
-
#symbolize_keys ⇒ Hash
Turn all keys into symbols.
-
#symbolize_keys! ⇒ Object
Turn all keys into symbols in place (destructive).
Instance Method Details
#deep_freeze ⇒ Hash
Freeze all values in a hash
10 11 12 13 14 15 16 17 |
# File 'lib/howzit/hash.rb', line 10 def deep_freeze chilled = {} each do |k, v| chilled[k] = v.is_a?(Hash) ? v.deep_freeze : v.freeze end chilled.freeze end |
#deep_freeze! ⇒ Object
Deep freeze a hash in place (destructive)
22 23 24 |
# File 'lib/howzit/hash.rb', line 22 def deep_freeze! replace deep_thaw.deep_freeze end |
#deep_thaw ⇒ Hash
Unfreeze nested hash values
31 32 33 34 35 36 37 38 |
# File 'lib/howzit/hash.rb', line 31 def deep_thaw chilled = {} each do |k, v| chilled[k] = v.is_a?(Hash) ? v.deep_thaw : v.dup end chilled.dup end |
#deep_thaw! ⇒ Object
Unfreeze nested hash values in place (destructive)
43 44 45 |
# File 'lib/howzit/hash.rb', line 43 def deep_thaw! replace deep_thaw end |
#stringify_keys ⇒ Hash
Turn all keys into string
51 52 53 |
# File 'lib/howzit/hash.rb', line 51 def stringify_keys each_with_object({}) { |(k, v), hsh| hsh[k.to_s] = v.is_a?(Hash) ? v.stringify_keys : v } end |
#stringify_keys! ⇒ Object
Turn all keys into strings in place (destructive)
58 59 60 |
# File 'lib/howzit/hash.rb', line 58 def stringify_keys! replace stringify_keys end |
#symbolize_keys ⇒ Hash
Turn all keys into symbols
66 67 68 |
# File 'lib/howzit/hash.rb', line 66 def symbolize_keys each_with_object({}) { |(k, v), hsh| hsh[k.to_sym] = v.is_a?(Hash) ? v.symbolize_keys : v } end |
#symbolize_keys! ⇒ Object
Turn all keys into symbols in place (destructive)
73 74 75 |
# File 'lib/howzit/hash.rb', line 73 def symbolize_keys! replace symbolize_keys end |