Skip to content

Instantly share code, notes, and snippets.

@stuffmc
Created September 17, 2011 12:29
Show Gist options
  • Save stuffmc/1223898 to your computer and use it in GitHub Desktop.
Save stuffmc/1223898 to your computer and use it in GitHub Desktop.
Ruby Implementation of "valueForKeyPath"
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
@stuffmc
Copy link
Author

stuffmc commented Sep 17, 2011

@impurist
Copy link

spot on!!!

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