CodeOfficer (owner)

Forks

Revisions

gist: 72592 Download_button fork
public
Public Clone URL: git://gist.github.com/72592.git
Embed All Files: show embed
try.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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")}"