public Method

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

Source Code

/*
*  call-seq:
*     exit(integer=0)
*     Kernel::exit(integer=0)
*     Process::exit(integer=0)
*  
*  Initiates the termination of the Ruby script by raising the
*  <code>SystemExit</code> 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"
*     
*  <em>produces:</em>
*     
*     rescued a SystemExit exception
*     after begin block
*     
*  Just prior to termination, Ruby executes any <code>at_exit</code> 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
*     
*  <em>produces:</em>
*     
*     at_exit function
*     in finalizer
*/

VALUE
rb_f_exit(argc, argv)
   int argc;
   VALUE *argv;
{
   VALUE status;
   int istatus;

   rb_secure(4);
   if (rb_scan_args(argc, argv, "01", &status) == 1) {
       switch (status) {
         case Qtrue:
           istatus = EXIT_SUCCESS;
           break;
         case Qfalse:
           istatus = EXIT_FAILURE;
           break;
         default:
           istatus = NUM2INT(status);
#if EXIT_SUCCESS != 0
           if (istatus == 0) istatus = EXIT_SUCCESS;
#endif
           break;
       }
   }
   else {
       istatus = EXIT_SUCCESS;
   }
   rb_exit(istatus);
   return Qnil;                /* not reached */
}
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.