Re: How to exit Ruby program properly


#1

Process.exit is the way to go (or just putting the rest of your logic
into the else section of that if statement).

There is nothing unclean about the fact that Process.exit raises an
exception. The fact that it raises an exception is noted to allow other
methods up the chain to intercept and possibly prevent the shutdown.

It also ensures that all the “ensure” thingies up the chain get
executed, so that you file output can be flushed and handles closed and
any other shutdown procedures can come into play. For example:

file = File.new(“outfile”,“w”)
begin
f.puts “Monkeys are cool”
Process.exit
ensure
f.close
puts “File flushed properly”
end

(Note that you’d usually pass a block to File.open, which does the
begin/ensure/close/end bit for you)

If you REALLY want to exit NOW, you can use Process.exit!, but generally
you don’t want to do that. Change the above the use Process.exit!, and
you’ll notice that “File flushed properly” will not be output - imagine
if there was a more important ensure clause hanging around.

I’m using Process.exit but the documentation implies that it
end

#####################################################################################
This email has been scanned by MailMarshal, an email content filter.
#####################################################################################