Skip to content

Instantly share code, notes, and snippets.

@CodeOfficer
Created March 2, 2009 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save CodeOfficer/72592 to your computer and use it in GitHub Desktop.
Save CodeOfficer/72592 to your computer and use it in GitHub Desktop.
# 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