Created
May 29, 2010 21:35
-
-
Save sixty4bit/418570 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 | |
# 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