Created
June 11, 2015 18:37
-
-
Save slowernet/b41289f67dfd972027f9 to your computer and use it in GitHub Desktop.
traverse a nested hash, yielding keypath and value
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
class Hash | |
# traverse a nested hash, yielding keypath and value | |
def walk(path = [], &block) | |
self.each do |k,v| | |
path.push(k) | |
v.is_a?(Hash) ? v.walk(path, &block) : yield(path, v) | |
path.pop | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment