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

    Join Date
    Dec 2006
    Posts
    1
    Rep Power
    0

    Hello, Newb Help please :)


    basically i started programming yesterday..
    and i have a quick question for anyone willing to help me out with what is probably a really newb poroblem.

    okay, here it is, loops...

    puts 'Hello Sonny Boy.'
    greeting = gets.chomp

    while greeting != greeting.upcase
    puts 'HUH?! SPEAK UP, SONNY.'
    greeting = gets.chomp
    end

    puts 'NO, NOT SINCE 1950!'
    puts 'Anything else you would like to say to me?'
    response = gets.chomp

    while response != response.upcase
    puts 'HUH?! SPEAK UP, SONNY!'
    response = gets.chomp
    end



    So is there a way i can add those loops together? man this is confusing... another thing im stuck on is I want my grandma in the program to not let me stop talking to her until i say bye in upcase letters, as you can see in this line (while response != response.upcase) but i tried adding the string 'bye' to that after the (!=), but i couldnt figure out which way would let me run the program. is there a way to do that so i cant stop talking to her until i type bye in upcase letters? thanks for reading about my brain stumps.

    Basically what im trying to do is:

    write a deaf grandma program. Whatever i say to grandma (whatever i type in), she should respond with:

    HUH?! SPEAK UP, SONNY!

    unless i shout it, (type in all capitals). If i shout, she can hear me and yells back:

    NO, NOT SINCE 1938.

    (see i have this part done, but.

    I want to not be able to stop talking to her until i shout bye (type bye in upcase letters).

    Thanks for your help.
  2. #2
  3. c0der
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2005
    Location
    Vancouver
    Posts
    665
    Rep Power
    155
    I don't consider this any simpler, though it's most definitely shorter:

    Code:
    until (line = gets.chomp) == "BYE"
      puts (line == line.upcase ? "NO, NOT SINCE 1983!" : "HUH?! SPEAK UP, SONNY!")
    end
    puts "SO LONG CHESTER! DON'T FORGET YOUR LEMONADE!"
    I'll do my best to explain what's going on in those four lines of ruby.

    • We enter an until loop. This executes the enclosed code until the condition (line = gets.chomp) == "BYE" is met
    • line = gets.chomp sets the variable line to input of the user's choosing (sans newline)
    • If the user typed "BYE" in all upper case letters the loop is complete, so Grandma says goodbye and the program exits
    • Otherwise the result of a [i]tertiary operator[i] ((line == line.upcase ? "NO, NOT SINCE 1983!" : "HUH?! SPEAK UP, SONNY!")) is printed to the screen and the test starts all over again. The syntax is probably a little scary looking to a novice, but it it's really just shorthand for a standard if/else construct:
      Code:
      if line == line.upcase
        puts "NO, NOT SINCE 1983!"
      else
        puts "HUH?! SPEAK UP, SONNY!"
      end
      The above code is trivial to break down (if the supplied line was a "SHOUT", say "NO, NOT SINCE 1983!", otherwise say "HUH?! SPEAK UP, SONNY") but I find it's a bit less elegant than the tertiary operator. That's just my subjective opinion though, take it for what it's worth.

IMN logo majestic logo threadwatch logo seochat tools logo