Created
February 28, 2012 16:19
-
-
Save wireframe/1933430 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
interesting. are you in ruby 1.8?
Yep
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Had to do this because the splat didn't play well with a second arg:
def deep_fetch(*keys)
last_key = keys.pop