Exiting in the middle of the program

  • Thread starter Helgitomas Gislason
  • Start date
H

Helgitomas Gislason

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 ...
 
P

Phrogz

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
 
T

Timothy Hunter

Helgitomas said:
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?
 
H

Helgitomas Gislason

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

?
 
S

Sylvain Abélard

7. if name == 'John'
8. puts 'Nice name!'
9. end
10.
11. if name == 'Mary'
12. puts 'What a beautiful name!'
13. 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).

http://www.zenspider.com/Languages/Ruby/QuickRef.html

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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top