#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    2
    Rep 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! =)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    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
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    @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 07:35 PM.
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    2
    Rep Power
    0
    ok, thankee very much! =)
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    383
    Rep Power
    0
    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.

IMN logo majestic logo threadwatch logo seochat tools logo