dakrone (owner)

Revisions

gist: 126620 Download_button fork
public
Description:
Nesting traps that untrap in Ruby
Public Clone URL: git://gist.github.com/126620.git
trapr
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
55
56
#!/usr/bin/env ruby
 
class Object
  def trapr_wrap(signal, newproc, function, *args)
    #puts "Trapping #{signal} with #{newproc.inspect}"
    oldproc = trap "#{signal}", newproc
    self.send("#{function}", *args)
  ensure
    #puts "Reseting trap. #{signal} -> #{oldproc.inspect}"
    trap "#{signal}", oldproc
  end
end
 
class Eggplant
  def initialize
  end
  
  def foo
    trap "SIGINT", Proc.new {
      puts "Trapped in foo!"
    }
    puts "I'm in foo for 5 seconds!"
    sleep(5);
    puts "Leaving foo."
  end
 
  def bar
    puts "I'm bar'n it up for 5 seconds!"
    sleep(5)
    puts "Done bar'n"
  end
 
end
 
trap "SIGINT", Proc.new { puts "Base trap." }
t = Eggplant.new
p = Proc.new { puts "Trapr'd!" }
# Trap bindings will be released when :foo exits
t.trapr_wrap "SIGINT", p, :foo
# Trap bindings will be released when :bar exits
t.trapr_wrap "SIGINT", p, :bar
puts "Back to base. T-minus 5."
sleep(5)
 
 
# [1:hinmanm@Xanadu:~/src/ruby/trapr]% ./trapr.rb
# I'm in foo for 5 seconds!
# ^CTrapped in foo!
# Leaving foo.
# I'm bar'n it up for 5 seconds!
# ^CTrapr'd!
# Done bar'n
# Back to base. T-minus 5.
# ^CBase trap.