Created
September 17, 2011 12:29
-
-
Save stuffmc/1223898 to your computer and use it in GitHub Desktop.
Ruby Implementation of "valueForKeyPath"
This file contains hidden or 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 | |
def key_path(dotted_path) | |
# see http://stackoverflow.com/questions/7139471/transform-a-ruby-hash-into-a-dotted-path-key-string | |
parts = dotted_path.split '.', 2 | |
match = self[parts[0]] | |
if !parts[1] or match.nil? | |
return match | |
else | |
return match.key_path(parts[1]) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Originally from http://stackoverflow.com/questions/7139471/transform-a-ruby-hash-into-a-dotted-path-key-string