Skip to content

Instantly share code, notes, and snippets.

@lucatironi
Created November 15, 2014 09:11
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 lucatironi/c3b59ea5176ab9874427 to your computer and use it in GitHub Desktop.
Save lucatironi/c3b59ea5176ab9874427 to your computer and use it in GitHub Desktop.
Retrieve a path in an Hash
def retrieve(path, hash)
remainder = hash.fetch(path.shift, nil)
remainder.is_a?(Hash) ? retrieve(path, remainder) : remainder
end
path = [:foo, :bar, :baz]
hash = { foo: { bar: { baz: 123 } } }
puts "path: #{path}"
puts "hash: #{hash}"
puts "result: #{retrieve(path, hash)}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment