Skip to content

Instantly share code, notes, and snippets.

@rf-
Created December 1, 2011 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rf-/1414750 to your computer and use it in GitHub Desktop.
Save rf-/1414750 to your computer and use it in GitHub Desktop.
# 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