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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#!/usr/bin/env ruby
require 'tins'
$:.unshift 'examples'
include Tins::Deflect
puts "Example 1"
deflect(NilClass, :method_missing, Deflector.new { nil }) do
begin
p "foo".bar.baz
rescue NoMethodError
p "caught 1"
end
p nil.bar.baz
t = Thread.new do
begin
p nil.bar.baz
rescue NoMethodError
p "caught 2"
end
end
t.join if t.alive?
p nil.bar.baz
end
begin
p nil.bar.baz
rescue NoMethodError
p "caught 3"
end
puts "-" * 70, "Example 2"
deflect_start(NilClass, :method_missing, Deflector.new { nil })
begin
p "foo".bar.baz
rescue NoMethodError
p "caught 1"
end
p nil.bar.baz
t = Thread.new do
begin
p nil.bar.baz
rescue NoMethodError
p "caught 2"
end
end
t.join if t.alive?
p nil.bar.baz
deflect_stop(NilClass, :method_missing)
begin
p nil.bar.baz
rescue NoMethodError
p "caught 3"
end
|