Gavin Kistner
3/19/2007 10:09:00 PM
On Mar 19, 4:05 pm, Helgitomas Gislason <nitrohel...@hotmail.com>
wrote:
> There is a ruby calculator in the /ruby installation folder which has
> the ability of that when you just type 'Exit', the program exits.
>
> How can I add this feature to a program built by myself? So when I type
> 'Exit' in the middle of the program, the program exits ...
RTFM :)
C:\>ri Kernel#exit
------------------------------------------------------------
Kernel#exit
exit(integer=0)
Kernel::exit(integer=0)
Process::exit(integer=0)
------------------------------------------------------------------------
Initiates the termination of the Ruby script by raising the
+SystemExit+ exception. This exception may be caught. The
optional
parameter is used to return a status code to the invoking
environment.
begin
exit
puts "never get here"
rescue SystemExit
puts "rescued a SystemExit exception"
end
puts "after begin block"
_produces:_
rescued a SystemExit exception
after begin block
Just prior to termination, Ruby executes any +at_exit+ functions
(see Kernel::at_exit) and runs any object finalizers (see
ObjectSpace::define_finalizer).
at_exit { puts "at_exit function" }
ObjectSpace.define_finalizer("string", proc { puts "in
finalizer" })
exit
_produces:_
at_exit function
in finalizer