Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/postageapp/utils.rb
Direct Known Subclasses
Instance Method Summary collapse
- #dig(*path) ⇒ Object
-
#recursive_stringify_keys! ⇒ Object
Destructively convert all keys to strings.
Instance Method Details
#dig(*path) ⇒ Object
Example usage:
@hash.dig(:k1) # same as @hash[:k1]
@hash.dig(:k1, :k2) # same as @hash[:k1] && @hash[:k1][:k2]
@hash.dig(:k1, :k2, k3) # same as @hash[:k1] && @hash[:k1][:k2] && @hash[:k1][:k2][:k3]
8 9 10 11 12 |
# File 'lib/postageapp/utils.rb', line 8 def dig(*path) path.inject(self) do |location, key| location.respond_to?(:keys) ? location[key] : nil end end |
#recursive_stringify_keys! ⇒ Object
Destructively convert all keys to strings.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/postageapp/utils.rb', line 17 def recursive_stringify_keys! keys.each do |key| value = delete(key) self[key.to_s] = case (value) when Hash value.recursive_stringify_keys! else value end end self end |