Skip to content

Instantly share code, notes, and snippets.

@wireframe
Created February 28, 2012 16:19
Show Gist options
  • Save wireframe/1933430 to your computer and use it in GitHub Desktop.
Save wireframe/1933430 to your computer and use it in GitHub Desktop.
# utility for performing multiple fetches on hashes
# ex:
# h = {:foo => {:bar => :baz}}
# h.deep_fetch :foo, :bar
class Hash
def deep_fetch(*keys, last_key)
scope = self
keys.each do |key|
scope = scope.fetch key, {}
end
scope[last_key]
end
end
@seanwalbran
Copy link

Had to do this because the splat didn't play well with a second arg:

def deep_fetch(*keys)
last_key = keys.pop

@wireframe
Copy link
Author

interesting. are you in ruby 1.8?

@seanwalbran
Copy link

Yep

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment