Created
April 29, 2009 20:37
-
-
Save leandro/104033 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
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