Created
May 12, 2009 14:48
-
-
Save rickw/110522 to your computer and use it in GitHub Desktop.
A different way to "try"
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
# | |
# method messing try a different way | |
# | |
class Object | |
def try | |
Try.new self | |
end | |
end | |
class Try | |
def initialize(scope_obj) | |
@obj = scope_obj | |
end | |
def method_missing(meth, *args, &block) | |
return @obj unless @obj.respond_to? meth | |
@obj.send meth, *args, &block | |
end | |
end | |
class Fred | |
def me | |
yield | |
"I am!" | |
end | |
end | |
f = Fred.new | |
puts f.try.you | |
puts f.try.me { puts "yay!" } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment