Skip to content

Instantly share code, notes, and snippets.

@leandro
Created April 29, 2009 20:37
Show Gist options
  • Save leandro/104033 to your computer and use it in GitHub Desktop.
Save leandro/104033 to your computer and use it in GitHub Desktop.
class Object
# with this method you can call stuffs like this Comment.nested_send(:creator, :login)
# it's the same of Comment.creator.login without the need of checking the absense of creator like:
# Comment.creator && Comment.creator.login
def nested_send(*methods)
obj = self
methods.each do |e|
case e
when Array
if obj.size > 1
obj = obj.send(e.first, *e[1..-1]) if obj.respond_to?(e.first)
else
obj = obj.send(e.first) if obj.respond_to?(e.first)
end
when Symbol
obj = obj.send(e) if obj.respond_to?(e)
end
return obj if obj.nil?
end
obj
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment