public
Last active

  • Download Gist
get_path.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
class Hash
def deep(*path)
path.inject(self) {|h,k| h.respond_to?(:fetch) ? h.fetch(k, nil) : h}
end
end
 
#example
stuff = {
:foo => {
:bar => nil
},
:bar => {
:baz => :whatever,
:what => {:ever => nil }
}
}
 
p stuff.deep(:foo, :bar) || stuff.deep(:bar, :baz)
p stuff.deep(:foo, :bar) || stuff.deep(:bar, :baz) || stuff.deep(:bar, :what, :ever) || "nothing"

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.