How to end script execution mid-script?

All

A control flow question: Could someone advise if there is command to end Ruby scripts operation? For example, if a certain condition is true, then the script should terminate?

Thanks.

Kernel::exit
----------------------------------------------------------- Kernel::exit
exit( anInteger=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 and
 runs any object finalizers (see ObjectSpace beginning on page ??).
    at_exit { puts "at_exit function" }
    ObjectSpace.define_finalizer("string",  proc { puts "in

finalizer" })
exit
produces:
at_exit function
in finalizer

----- Original Message -----
From: “Kurt Euler” keuler@portal.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Friday, August 29, 2003 4:38 PM
Subject: How to end script execution mid-script?

All

A control flow question: Could someone advise if there is command to end
Ruby scripts operation? For example, if a certain condition is true, then
the script should terminate?

Thanks.

All

A control flow question: Could someone advise if there is command to end Ruby scripts operation? For example, if a certain condition is true, then the script should terminate?

if condition?
exit
end

···

On Fri, 2003-08-29 at 17:38, Kurt Euler wrote:

All

A control flow question: Could someone advise if there is command to
end Ruby scripts operation? For example, if a certain condition is
true, then the script should terminate?

~$ irb

Process.kill(“TERM”, Process.pid)
Terminated
~$

:slight_smile:

Or you could just do the sane thing and use the ‘exit’ method as
suggested by other posters.

Jason Creighton

···

On Sat, 30 Aug 2003 08:38:15 +0900 Kurt Euler keuler@portal.com wrote:

----------------------------------------------------------- Kernel::exit
exit( anInteger=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.

---------------------------------------------------------- Kernel::abort
abort

 Terminate execution immediately, effectively by calling
 Kernel.exit(1).



   
 Kurt Euler wrote:

All

A control flow question: Could someone advise if there is command to end Ruby scripts operation? For example, if a certain condition is true, then the script should terminate?

Thanks.