This post originated from an RSS feed registered with Ruby Buzz
by Daniel Berger.
Original Post: $SIG{"RUBY"}
Feed Title: Testing 1,2,3...
Feed URL: http://djberg96.livejournal.com/data/rss
Feed Description: A blog on Ruby and other stuff.
One of the things I miss from Perl when I'm using Ruby are the global $SIG{WARN} and $SIG{DIE} handlers. For reports & other various things, my typical idiom was this (using a bit of pseudo code):
# Avoid in combination with eval
$SIG{DIE}= sub {
# Log error
# Send out email to interested parties
}
This can be done in Ruby by overriding the Exception class. Easy enough:
# Totally untested
class Exception
@@proc_handler = nil
def initialize
# old initialize
# @@proc_handler.call if @@proc_handler
end
def self.proc_handler=(proc_obj)
@@proc_handler = proc_obj
end
def self.proc_handler
@@proc_handler
end
end
But, this is a pain to have to do for every report generation script I have. I think I might wrap this in a simple, generic package that folks can use. Thus, your cod would look something like this:
Exception.proc_handler = lambda{
# Log error
# Send out email to interested parties
}
Then, whenver an exception occurs anywhere in your code it will call the proc you've assigned. At least, I think it will.