Class: Hash
Instance Method Summary collapse
-
#fetch_nested(*keys) ⇒ Object
(also: #dig)
nil safe version of Hash#[].
Instance Method Details
#fetch_nested(*keys) ⇒ Object Also known as: dig
nil safe version of Hash#[]. h.fetch_nested(*) is basically the same as h.try.send(:[],‘world’).
4 5 6 7 8 9 10 |
# 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 |