Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/postageapp/utils.rb

Direct Known Subclasses

PostageApp::Mailer::Attachments

Instance Method Summary collapse

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