Skip to content

Instantly share code, notes, and snippets.

@pmarreck
Last active August 29, 2015 14:16
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 pmarreck/94ec59e2ee2925daa06f to your computer and use it in GitHub Desktop.
Save pmarreck/94ec59e2ee2925daa06f to your computer and use it in GitHub Desktop.
Deep-index a Ruby hash
class Hash
def get(*keys)
keys.inject(self){|h, k| h[k] if h}
end
end
h = {:a=>{:b=>{:c=>"d"}}}
puts h.get(:a).inspect
puts h.get(:a, :b).inspect
puts h.get(:a, :b, :c).inspect
puts h.get(:a, :d, :c).inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment