Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# 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
You can’t perform that action at this time.