class Hash
Public Instance Methods
fetch_nested(*keys) { |*keys| ... }
click to toggle source
nil safe version of Hash#[].
h.fetch_nested(*['hello','world']) is basically the same as h['hello'].try.send(:[],'world').
# File lib/hash/fetch_nested.rb, line 4 def fetch_nested(*keys) begin keys.reduce(self){|accum, k| accum.fetch(k)} rescue (RUBY_VERSION<'1.9' ? IndexError : KeyError) block_given? ? yield(*keys) : nil end end
Also aliased as: dig