# Ensure statement and signals

**URL:** <https://www.ruby-forum.com/t/ensure-statement-and-signals/97443>\
**Category:** Ruby\
**Created:** [January 25, 2006, 11:06pm UTC](https://www.ruby-forum.com/t/ensure-statement-and-signals/97443 "2006-01-25T23:06:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tsuraan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8797f3/32.png) [@tsuraan](https://www.ruby-forum.com/u/tsuraan)\
**Post date:** [January 25, 2006, 11:06pm UTC](https://www.ruby-forum.com/t/ensure-statement-and-signals/97443/1 "2006-01-25T23:06:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![tsuraan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8797f3/32.png) [@tsuraan](https://www.ruby-forum.com/u/tsuraan)\
**Post date:** [January 25, 2006, 11:09pm UTC](https://www.ruby-forum.com/t/ensure-statement-and-signals/97443/2 "2006-01-25T23:09:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tsuraan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8797f3/32.png) [@tsuraan](https://www.ruby-forum.com/u/tsuraan)\
**Post date:** [January 26, 2006, 11:45am UTC](https://www.ruby-forum.com/t/ensure-statement-and-signals/97443/3 "2006-01-26T11:45:52Z")

</div>

On Thu, 2006-01-26 at 07:06 +0900, tsuraan wrote:

> hup, etc). Nothing makes the program print “hi there!” Can this be  
> done?

Running your code and hitting Ctrl-C does give the expected behaviour,  
as does sending an INT directly:

[rosco@jukebox ruby]$ ruby term1.rb &  
[1] 28052  
[rosco@jukebox ruby]$ kill -INT 28052  
[rosco@jukebox ruby]$  
“hi there!”  
term1.rb:2:in `sleep’: Interrupt  
from term1.rb:2

[1]+ Exit 1 ruby term1.rb

(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

Running that gives:

[rosco@jukebox ruby]$ ruby term2.rb &  
[1] 28088  
[rosco@jukebox ruby]$ kill -TERM 28088  
[rosco@jukebox ruby]$  
Got Terminated!  
hi there!

[1]+ Done ruby term2.rb

Obviously it’s pretty simplistic but maybe gives a starting point…

Hope that helps,  
Ross

---

<div class="post-metadata">

**Author:** ![tsuraan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8797f3/32.png) [@tsuraan](https://www.ruby-forum.com/u/tsuraan)\
**Post date:** [January 26, 2006, 12:09pm UTC](https://www.ruby-forum.com/t/ensure-statement-and-signals/97443/4 "2006-01-26T12:09:58Z")

</div>

On Thu, 2006-01-26 at 07:08 +0900, tsuraan wrote:

> 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
