Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 13th, 2010, 01:57 PM
doomxorz doomxorz is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2010
Posts: 2 doomxorz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 38 m 50 sec
Reputation Power: 0
Question A Program ending command

Hello, I am new to ruby and programming in general and just need a bit of help.

Basically all i need is to know if there is a single command that will end the program. for example...

puts 'End program? (Y/N)'
cmd = gets.chomp
if cmd.downcase = 'y'
<command I need that ends the program>
end
if cmd.downcase = 'n'
puts 'You chose to not to end the program.'
end

<program continues>

Hmmm i guess that makes sense...
Any help would be appreciated! =)

Reply With Quote
  #2  
Old July 13th, 2010, 03:47 PM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 989 L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 22 h 45 m 6 sec
Reputation Power: 362
Send a message via AIM to L7Sqr
You are looking for the exit call. See example below.
Code:
#!/usr/bin/env ruby


print ("Kill myself? ");
$stdout.flush

case gets.chomp
when /^y/i
    exit 0
else
    puts "Yay!"
end

puts "Still not dead"
Comments on this post
delnan disagrees: No sure whether to agree or not - seriosuly, a regex for one damned letter?!?
__________________
True happiness is not getting what you want, it's wanting what you've already got.

My Blog

Reply With Quote
  #3  
Old July 13th, 2010, 06:32 PM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 989 L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 22 h 45 m 6 sec
Reputation Power: 362
Send a message via AIM to L7Sqr
@delnan:

The regex handles the class of inputs that begin with the letter y in a case insensitive way. You know: Yes, yeah, y, yuppers, yesterday, Yummy.

Perhaps something along the lines of
Code:
exit unless getc != 121
would have been better?

I figured something the OP could digest and play with would be appropriate. Although, I do see the traces of a context switch in there: parenthesis and a semicolon. *shudders*

Last edited by L7Sqr : July 13th, 2010 at 06:35 PM.

Reply With Quote
  #4  
Old July 13th, 2010, 11:56 PM
doomxorz doomxorz is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2010
Posts: 2 doomxorz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 38 m 50 sec
Reputation Power: 0
ok, thankee very much! =)

Reply With Quote
  #5  
Old July 14th, 2010, 05:23 AM
delnan delnan is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2009
Posts: 383 delnan User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)delnan User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)delnan User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)delnan User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)delnan User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)delnan User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)delnan User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)delnan User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)delnan User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)delnan User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)delnan User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Week 7 h 31 m
Reputation Power: 0
Quote:
The regex handles the class of inputs that begin with the letter y in a case insensitive way. You know: Yes, yeah, y, yuppers, yesterday, Yummy.

Yeah, I know. But if it asks for 'y' or 'n', I feel like it should only accept these - case insensetively of course - and not treat "yikes" as 'y'.
The OP's solution on that is good if you ask me - accepts both 'y' and 'Y' and treats everything else as 'n' - perfectly simple semantics.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > A Program ending command

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap