Skip to content

Instantly share code, notes, and snippets.

@joshuaflanagan
Created October 11, 2010 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshuaflanagan/620536 to your computer and use it in GitHub Desktop.
Save joshuaflanagan/620536 to your computer and use it in GitHub Desktop.
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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment