# How do I exit my program at any time?

**URL:** <https://www.ruby-forum.com/t/how-do-i-exit-my-program-at-any-time/113997>\
**Category:** Ruby\
**Created:** [September 6, 2007, 11:15pm UTC](https://www.ruby-forum.com/t/how-do-i-exit-my-program-at-any-time/113997 "2007-09-06T23:15:56Z")\
**Posts on this page:** 6\
**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:** [September 6, 2007, 11:15pm UTC](https://www.ruby-forum.com/t/how-do-i-exit-my-program-at-any-time/113997/1 "2007-09-06T23:15:56Z")

</div>

Hello. When it comes to big, complex and long programs, you don’t always  
want to be needing to say at every single string:

if answer == ‘exit’  
exit  
end

Get the point? Let’s say you’re making a program that’s kinda like this:

* * *

puts ‘Hello, my name is Jimmy Hendrix. What’s your name?’  
puts ’ ’

name = gets.chomp

## If name == ‘Charlie’ or name == ‘Matt’ puts ‘Ahh, great to see you. You’re a boy.’ if name == ‘Ann’ or name == ‘Sandria’ puts ‘I see, you’re a girl.’ end

Now, if I wanted to exit my program lets say when I’m supposed to say  
the name, I would be needing to do a, if name == ‘exit’, exit. and the  
same thing if I wanted to exit when I was deeper in a longer program.

The main point I’m trying to get to here is: "Is there any function that  
I can put at the beginning or the end, that says my program to exit  
whenever I say exit WITHOUT having to put the:

if name == ‘exit’  
exit  
end

at every string I want to have the possibility yo exit from??

---

<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:** [September 6, 2007, 11:25pm UTC](https://www.ruby-forum.com/t/how-do-i-exit-my-program-at-any-time/113997/2 "2007-09-06T23:25:26Z")

</div>

On 9/7/07, Helgitomas G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello. When it comes to big, complex and long programs, you don’t always  
> want to be needing to say at every single string:
> 
> if answer == ‘exit’  
> exit  
> end

def get\_input(prompt)  
puts prompt  
retval = gets.chomp  
if retval == ‘exit’  
exit  
else  
return retval  
end  
end

a = get\_input “Enter your name”

# etc.

martin

---

<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:** [September 6, 2007, 11:26pm UTC](https://www.ruby-forum.com/t/how-do-i-exit-my-program-at-any-time/113997/3 "2007-09-06T23:26:26Z")

</div>

On Sep 6, 3:15 pm, Helgitomas G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> The main point I’m trying to get to here is: "Is there any function that  
> I can put at the beginning or the end, that says my program to exit  
> whenever I say exit WITHOUT having to put the:
> 
> if name == ‘exit’  
> exit  
> end
> 
> at every string I want to have the possibility yo exit from??

class Object  
def gets( _args )  
result = super  
exit if result =~ /\A\s_exit\s\*\z/oi  
result  
end  
end

However, instead of monkey-patching the object class (and shadowing)  
the Kernel#gets method), I would suggest instead writing your own  
method:

def gets\_or\_exit  
result = gets  
exit if result =~ /\A\s_exit\s_\z/oi  
result  
end

This gives you the ability to decide if there’s a time when you want  
to use gets without exiting, and to do additional common work (like  
always #chomp the result before returning it).

---

<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:** [September 6, 2007, 11:31pm UTC](https://www.ruby-forum.com/t/how-do-i-exit-my-program-at-any-time/113997/4 "2007-09-06T23:31:26Z")

</div>

On 9/6/07, Helgitomas G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The main point I’m trying to get to here is: "Is there any function that  
> I can put at the beginning or the end, that says my program to exit  
> whenever I say exit WITHOUT having to put the:

Redefine gets to do what you want …

module Kernel  
def gets( sep = $/ )  
str = super  
str.chomp!  
exit if str == ‘exit’  
str  
end  
end

Or if you are not comfortable with redefining methods in Kernel, write  
your own gets method …

def gets\_and\_exit( sep = $/ )  
str = gets( sep )  
…  
end

Blessings,  
TwP

---

<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:** [September 7, 2007, 1:13am UTC](https://www.ruby-forum.com/t/how-do-i-exit-my-program-at-any-time/113997/5 "2007-09-07T01:13:33Z")

</div>

wow, I like their ideas better. I was gonna say, put the whole program  
into a huge loop. I guess I need to start thinking on an easier  
level 🙂

~Jeremy

On Sep 6, 2:53 pm, Helgitomas G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<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:** [September 6, 2007, 11:53pm UTC](https://www.ruby-forum.com/t/how-do-i-exit-my-program-at-any-time/113997/6 "2007-09-06T23:53:32Z")

</div>

Thank you very much, people. I really appriciate it.
