# Exiting in the middle of the program

**URL:** <https://www.ruby-forum.com/t/exiting-in-the-middle-of-the-program/89366>\
**Category:** Ruby\
**Created:** [March 19, 2007, 11:05pm UTC](https://www.ruby-forum.com/t/exiting-in-the-middle-of-the-program/89366 "2007-03-19T23:05:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![helgso](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/dec6dc/32.png) [@helgso](https://www.ruby-forum.com/u/helgso)\
**Post date:** [March 19, 2007, 11:05pm UTC](https://www.ruby-forum.com/t/exiting-in-the-middle-of-the-program/89366/1 "2007-03-19T23:05:46Z")

</div>

Good-Day to you all, programmers!

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 …

---

<div class="post-metadata">

**Author:** ![helgso](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/dec6dc/32.png) [@helgso](https://www.ruby-forum.com/u/helgso)\
**Post date:** [March 19, 2007, 11:12pm UTC](https://www.ruby-forum.com/t/exiting-in-the-middle-of-the-program/89366/2 "2007-03-19T23:12:07Z")

</div>

On Mar 19, 4:05 pm, Helgitomas G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

```

---

<div class="post-metadata">

**Author:** ![helgso](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/dec6dc/32.png) [@helgso](https://www.ruby-forum.com/u/helgso)\
**Post date:** [March 19, 2007, 11:22pm UTC](https://www.ruby-forum.com/t/exiting-in-the-middle-of-the-program/89366/3 "2007-03-19T23:22:09Z")

</div>

Helgitomas G. wrote:

> Good-Day to you all, programmers!
> 
> 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 …

I’m pretty sure those examples are there just so you can see how to do  
things with Ruby. Why don’t you just look at the program, figure out how  
it’s doing it, and then do it the same way in your program?

---

<div class="post-metadata">

**Author:** ![helgso](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/dec6dc/32.png) [@helgso](https://www.ruby-forum.com/u/helgso)\
**Post date:** [March 21, 2007, 7:29pm UTC](https://www.ruby-forum.com/t/exiting-in-the-middle-of-the-program/89366/4 "2007-03-21T19:29:40Z")

</div>

Errm… That does not tell me what code to put in the program to exit  
it…

Example:

1. puts ‘Hello there’
2. puts ‘What’s your name?’
3. puts ’ ’
4. name = gets.chomp
5. puts ’ ’
6. 
7. if name == ‘John’
8. puts ‘Nice name!’
9. end
10. 
11. if name == ‘Mary’
12. puts ‘What a beautiful name!’
13. end

Now. If I’d like to exit the program without typing the name, just say  
“exit”, what code should I put?

Can I only say:

if name == ‘exit’  
exit  
end

?

---

<div class="post-metadata">

**Author:** ![helgso](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/dec6dc/32.png) [@helgso](https://www.ruby-forum.com/u/helgso)\
**Post date:** [March 21, 2007, 9:59pm UTC](https://www.ruby-forum.com/t/exiting-in-the-middle-of-the-program/89366/5 "2007-03-21T21:59:03Z")

</div>

> > if name == ‘exit’  
> > exit  
> > end
> 
> Yes. But why didn’t you try ? You’d have seen by yourself 🙂

Haha, lol. I will try the case thingy =D THX

---

<div class="post-metadata">

**Author:** ![helgso](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/dec6dc/32.png) [@helgso](https://www.ruby-forum.com/u/helgso)\
**Post date:** [March 21, 2007, 8:07pm UTC](https://www.ruby-forum.com/t/exiting-in-the-middle-of-the-program/89366/6 "2007-03-21T20:07:22Z")

</div>

> 1. if name == ‘John’
> 2. puts ‘Nice name!’
> 3. end
> 4. 
> 5. if name == ‘Mary’
> 6. puts ‘What a beautiful name!’
> 7. end

Wow, I really recommend you look around to the “case” statement.  
And the ‘star’ (\*) operator (this one is tricky : it can either make  
an array from separated vars or splat an existing array into several  
separate stuff).

> **[Ruby | zenspider.com | by ryan davis](https://www.zenspider.com/ruby/)**
>
> Ruby facts, opinions, source code, links, and stuff.

case name  
when “John”, “Johnny” ; puts “nice name”  
when \* girls\_name ; puts “beautiful name”  
when “exit” ; exit 0  
else puts name  
end

> if name == ‘exit’  
> exit  
> end

Yes. But why didn’t you try ? You’d have seen by yourself 🙂

---

<div class="post-metadata">

**Author:** ![helgso](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/dec6dc/32.png) [@helgso](https://www.ruby-forum.com/u/helgso)\
**Post date:** [March 21, 2007, 10:04pm UTC](https://www.ruby-forum.com/t/exiting-in-the-middle-of-the-program/89366/7 "2007-03-21T22:04:40Z")

</div>

Yes,

if name == ‘exit’  
exit  
end

will get you an exit, or you can also put ‘’ without the word exit and  
an empty entry will exit the program as well.

code:  
if name = = ’ ’  
exit  
end

_~Kay~_
