Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sixty4bit
Created May 29, 2010 21:35
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 sixty4bit/418570 to your computer and use it in GitHub Desktop.
Save sixty4bit/418570 to your computer and use it in GitHub Desktop.
class Object
# call send on nested objects.
# example: nested_send(blog_post, 'author.name') #=> 'Bob'
# returns nil if any call returns nil
def nested_send(method_name)
val = self
method_names = method_name.split('.')
method_names.each do |m|
return nil unless val
val = val.send m.to_sym
end
val
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment