-
-
Save dallas/143198 to your computer and use it in GitHub Desktop.
TryProxy by CodeOfficer from heypanda.com
This file contains hidden or 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
# courtesy of http://heypanda.com/ | |
class TryProxy | |
def initialize(receiving_object) | |
@receiving_object = receiving_object | |
end | |
def method_missing(meth, *args, &block) | |
@receiving_object.nil? ? nil : @receiving_object.send(meth, *args, &block) rescue nil | |
end | |
end | |
class Object | |
def try | |
TryProxy.new(self) | |
end | |
end | |
class Cat | |
def speak(what) | |
what | |
end | |
end | |
cat = Cat.new | |
puts "The cat doesn't say: #{cat.try.say(:hello)}" | |
puts "The cat tries to say: #{cat.try.speak("BAI FOO")}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment