I have a program (currently written in python, but that could change
that runs until it recieves a signal to exit. Before exiting, it
needs to close a Lucene index, so the logical thing to do would be put
that writer.close call in an ensure statement so that the index will
close before the program exits. It looks like ruby (python as well)
doesnât integrate external signals in the normal program flow at all.
Is it possible to have a signal trigger an exception so that an ensure
statement will be executed? My test code is:
#!/usr/bin/env ruby
begin
sleep(1000)
ensure
p âhi there!â
end
And I run the script and send it all sort of signals (quit, int, term,
hup, etc). Nothing makes the program print âhi there!â Can this be
done?
Wow, Iâm not smart. I had print in my actual test code, which doesnât
do a newline. The message was lost in my ugly command prompt. So, it
looks like Ruby handles this very nicely (with a SignalException).
Sorry for the noise.
(I made it p â\nhi there!â to make the example clearer). If you wanted
to broaden your horizons a bit you could try trapping some signals and
raising exceptions yourself:
trap(âTERMâ) {
raise âTerminated!â
}
begin
sleep 1000
rescue Exception => ex
puts â\nGot #{ex}â
ensure
puts âhi there!â
end
Wow, Iâm not smart. I had print in my actual test code, which doesnât
do a newline. The message was lost in my ugly command prompt. So, it
looks like Ruby handles this very nicely (with a SignalException).
Sorry for the noise.
Doh, sorry for the reply to the noise
self.lookahead += 2
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.