Created
December 1, 2011 07:49
-
-
Save rf-/1414750 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
# Recursively retrieve nested attributes (or method calls) from an object. | |
def get_path(obj, *path) | |
if obj.is_a?(Array) | |
obj.map { |o| get_path(o, *path) } | |
else | |
if path.count > 1 | |
next_obj = obj.send(path.first) | |
get_path(next_obj, *path.drop(1)) | |
else | |
obj.send(path.first) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment