[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: How to exit Ruby program properly

Daniel Sheppard

12/13/2005 5:01:00 AM

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.


> -----Original Message-----
> From: list-bounce@example.com
> [mailto:list-bounce@example.com] On Behalf Of Jeff Cohen
> Sent: Tuesday, 13 December 2005 2:57 PM
> To: ruby-talk ML
> Subject: How to exit Ruby program properly
>
> I'm learning to write ruby code, and I'm using 1.8.2 on Windows XP.
> What is the best way to exit from my .rb script?
>
> I'm using Process.exit but the documentation implies that it
> will throw
> an exception. So I'm guessing there's a "cleaner" way to exit?
>
> For example:
>
> test.rb
> ====================
> if ARGV.length == 0
> puts "Please specify a filename"
> Process.exit
> end
>
> # script continues here...
>
>
> Thanks!
> Jeff
>
> --
> Posted via http://www.ruby-....
>
>
>
#####################################################################################
This email has been scanned by MailMarshal, an email content filter.
#####################################################################################