Skip to content

Instantly share code, notes, and snippets.

@dallas
Forked from CodeOfficer/try.rb
Created July 8, 2009 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dallas/143198 to your computer and use it in GitHub Desktop.
Save dallas/143198 to your computer and use it in GitHub Desktop.
TryProxy by CodeOfficer from heypanda.com
# 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