Ruby Nuby Question

Discussion in 'Ruby' started by Nigama Xx, Dec 7, 2007.

  1. Nigama Xx

    Nigama Xx Guest

    Hello,

    I am just learning Ruby, via several resources (online: Why's,
    downloaded PDF's, and Chris Pine's book "Learn to Program Ruby"). I
    finished Chris Pine's book and was going back through doing any of the
    exercises I hadn't done the first time and trying to mess with/break the
    programs I had done the first time through.

    In looking back over my code and trying to clean things up, I managed to
    think of two good questions I can't seem to find the answer to
    elsewhere. I figured this was the brightest and smartest group of
    Ruby-heads out there. :)

    I'll ask my questions first and put the code from the program down
    below. The assignment asked us to create a "Deaf Grandmother" program,
    which you could only get out of by saying "BYE" to in all caps, three
    times.

    The first question I had was, for some reason, if you input "BYE!" for
    the second or third required "BYE" (to exit/end the program), it
    launches into an infinite loop. Why is that? BYE works correctly.
    What is it about "BYE!"?

    Second question is... what if I wanted the program to require you to say
    "BYE" three times in a row instead of just three times throughout the
    conversation. Is there any easy way to do this?


    Okay, thanks for your time, the code from the program is below..

    Code:
    talk = 'I <3 my grandmother'
    puts 'WHAT A FINE YOUNG GRANDCHILD YOU ARE, COME TO VISIT YOUR DEAR OLD
    GRANNY!  COME IN, COME IN!'
    
    while talk != 'BYE'
    talk = gets.chomp
    if talk != talk.upcase
    puts 'HUH?!  SPEAK UP, SONNY!'
    elsif talk == talk.upcase && talk != 'BYE'
    year = 1
    while year < 30
    year = rand(51)
    end
    puts 'NO, NOT SINCE 19' + year.to_s + '!'
    end
    end
    
    if talk == 'BYE'
    puts 'WHAT WAS THAT?'
    talk = gets.chomp
    while talk != 'BYE'
    if talk != talk.upcase
    puts 'HUH?!  SPEAK UP, SONNY!'
    elsif talk == talk.upcase && talk != 'BYE'
    year = 1
    while year < 30
    year = rand(51)
    end
    puts 'NO, NOT SINCE 19' + year.to_s + '!'
    end
    end
    
    if talk == 'BYE'
    puts 'EYE?  WHAT\'S WRONG WITH YOUR EYE?'
    talk = gets.chomp
    while talk != 'BYE'
    if talk != talk.upcase
    puts 'HUH?!  SPEAK UP, SONNY!'
    elsif talk == talk.upcase && talk != 'BYE'
    year = 1
    while year < 30
    year = rand(51)
    end
    puts 'NO, NOT SINCE 19' + year.to_s + '!'
    end
    end
    
    if talk == 'BYE'
    puts 'OH VERY WELL, SEE YOU ANOTHER TIME, THEN!'
    end
    end
    end
    
    --
    Posted via http://www.ruby-forum.com/.
     
    Nigama Xx, Dec 7, 2007
    #1
    1. Advertising

  2. On Dec 7, 2007, at 2:12 PM, Nigama Xx wrote:

    > The first question I had was, for some reason, if you input "BYE!" for
    > the second or third required "BYE" (to exit/end the program), it
    > launches into an infinite loop. Why is that? BYE works correctly.
    > What is it about "BYE!"?


    <snip>

    > if talk == 'BYE'
    > puts 'EYE? WHAT\'S WRONG WITH YOUR EYE?'
    > talk = gets.chomp
    > while talk != 'BYE'
    > if talk != talk.upcase
    > puts 'HUH?! SPEAK UP, SONNY!'
    > elsif talk == talk.upcase && talk != 'BYE'
    > year = 1
    > while year < 30
    > year = rand(51)
    > end
    > puts 'NO, NOT SINCE 19' + year.to_s + '!'
    > end
    > end
    >
    > if talk == 'BYE'
    > puts 'OH VERY WELL, SEE YOU ANOTHER TIME, THEN!'
    > end
    > end



    Nothing is really special about "BYE!". Because you don't ask for
    input within the while-loop, any response other than "BYE" will put
    it into an infinite loop. The following revision will work.

    <code>
    if talk == 'BYE'
    puts 'EYE? WHAT\'S WRONG WITH YOUR EYE?'
    talk = gets.chomp
    while talk != 'BYE'
    if talk != talk.upcase
    puts 'HUH?! SPEAK UP, SONNY!'
    elsif talk == talk.upcase && talk != 'BYE'
    year = 1
    while year < 30
    year = rand(51)
    end
    puts 'NO, NOT SINCE 19' + year.to_s + '!'
    end
    talk = gets.chomp # <--- new code here
    end
    if talk == 'BYE'
    puts 'OH VERY WELL, SEE YOU ANOTHER TIME, THEN!'
    end
    end
    </code>

    Also you might be interested in simplifying your code -- for example:

    <code>
    if talk == 'BYE'
    puts 'EYE? WHAT\'S WRONG WITH YOUR EYE?'
    talk = gets.chomp
    while talk != 'BYE'
    if talk != talk.upcase
    puts 'HUH?! SPEAK UP, SONNY!'
    else
    puts "NO, NOT SINCE #{1930 + rand(21)}!"
    end
    talk = gets.chomp
    end
    puts 'OH VERY WELL, SEE YOU ANOTHER TIME, THEN!'
    end
    </code>

    > Second question is... what if I wanted the program to require you
    > to say
    > "BYE" three times in a row instead of just three times throughout the
    > conversation. Is there any easy way to do this?



    Depends what you mean by easy. I don't think it would be difficult,
    but it would require a fair amount of rewriting. Don't have the time
    now to go into the details. I'm sorry.

    Regards, Morton
     
    Morton Goldberg, Dec 8, 2007
    #2
    1. Advertising

  3. On Dec 7, 1:12 pm, Nigama Xx <> wrote:
    > Hello,
    >
    > I am just learning Ruby, via several resources (online: Why's,
    > downloaded PDF's, and Chris Pine's book "Learn to Program Ruby"). I
    > finished Chris Pine's book and was going back through doing any of the
    > exercises I hadn't done the first time and trying to mess with/break the
    > programs I had done the first time through.
    >
    > In looking back over my code and trying to clean things up, I managed to
    > think of two good questions I can't seem to find the answer to
    > elsewhere. I figured this was the brightest and smartest group of
    > Ruby-heads out there. :)
    >
    > I'll ask my questions first and put the code from the program down
    > below. The assignment asked us to create a "Deaf Grandmother" program,
    > which you could only get out of by saying "BYE" to in all caps, three
    > times.
    >
    > The first question I had was, for some reason, if you input "BYE!" for
    > the second or third required "BYE" (to exit/end the program), it
    > launches into an infinite loop. Why is that? BYE works correctly.
    > What is it about "BYE!"?
    >
    > Second question is... what if I wanted the program to require you to say
    > "BYE" three times in a row instead of just three times throughout the
    > conversation. Is there any easy way to do this?
    >
    > Okay, thanks for your time, the code from the program is below..
    >
    >
    Code:
    > talk = 'I <3 my grandmother'
    > puts 'WHAT A FINE YOUNG GRANDCHILD YOU ARE, COME TO VISIT YOUR DEAR OLD
    > GRANNY!  COME IN, COME IN!'
    >
    > while talk != 'BYE'
    > talk = gets.chomp
    >   if talk != talk.upcase
    >     puts 'HUH?!  SPEAK UP, SONNY!'
    >   elsif talk == talk.upcase && talk != 'BYE'
    >     year = 1
    >     while year < 30
    >       year = rand(51)
    >     end
    >     puts 'NO, NOT SINCE 19' + year.to_s + '!'
    >   end
    > end
    >
    > if talk == 'BYE'
    >   puts 'WHAT WAS THAT?'
    >   talk = gets.chomp
    >   while talk != 'BYE'
    >       if talk != talk.upcase
    >         puts 'HUH?!  SPEAK UP, SONNY!'
    >       elsif talk == talk.upcase && talk != 'BYE'
    >         year = 1
    >         while year < 30
    >           year = rand(51)
    >         end
    >         puts 'NO, NOT SINCE 19' + year.to_s + '!'
    >       end
    >   end
    >
    >   if talk == 'BYE'
    >       puts 'EYE?  WHAT\'S WRONG WITH YOUR EYE?'
    >       talk = gets.chomp
    >       while talk != 'BYE'
    >           if talk != talk.upcase
    >             puts 'HUH?!  SPEAK UP, SONNY!'
    >           elsif talk == talk.upcase && talk != 'BYE'
    >             year = 1
    >             while year < 30
    >               year = rand(51)
    >             end
    >             puts 'NO, NOT SINCE 19' + year.to_s + '!'
    >           end
    >       end
    >
    >       if talk == 'BYE'
    >         puts 'OH VERY WELL, SEE YOU ANOTHER TIME, THEN!'
    >       end
    >   end
    > end
    > 
    > --
    > Posted viahttp://www.ruby-forum.com/.



    history = []
    bye_replies = [
    'WHAT WAS THAT?',
    "EYE? WHAT'S WRONG WITH YOUR EYE?"
    ]

    puts 'WHAT A FINE YOUNG GRANDCHILD YOU ARE, COME TO VISIT
    YOUR DEAR OLD GRANNY! COME IN, COME IN!'

    bye_count = 0

    while true
    talk = gets.strip
    history << talk

    if talk != talk.upcase
    puts 'HUH?! SPEAK UP, SONNY!'
    elsif "BYE" == talk
    break if ['BYE'] * 3 == history[ -3 .. -1 ]
    puts bye_replies[ bye_count % bye_replies.size ]
    bye_count += 1
    else
    year = rand( 21 ) + 30
    puts "NO, NOT SINCE 19#{ year }!"
    end
    end

    puts 'OH VERY WELL, SEE YOU ANOTHER TIME, THEN!'
     
    William James, Dec 8, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexey Verkhovsky
    Replies:
    8
    Views:
    162
    Hal Fulton
    Jul 18, 2004
  2. Edwin Eyan Moragas

    nuby question: question marks in method names

    Edwin Eyan Moragas, Aug 30, 2004, in forum: Ruby
    Replies:
    0
    Views:
    139
    Edwin Eyan Moragas
    Aug 30, 2004
  3. Alexey Verkhovsky

    Ruby-Nuby forum

    Alexey Verkhovsky, Sep 7, 2004, in forum: Ruby
    Replies:
    48
    Views:
    384
    Bill Kelly
    Sep 9, 2004
  4. Alexey Verkhovsky

    Ruby-Nuby forum

    Alexey Verkhovsky, Sep 8, 2004, in forum: Ruby
    Replies:
    21
    Views:
    266
    James Britt
    Sep 9, 2004
  5. Replies:
    2
    Views:
    132
Loading...

Share This Page