Strange Ruby Issue in SciTE - Won't Parse First Input

NOTE: I posted this in the RForum category accidentally, and it seems
very dead there so here I am.

Yeah, I know. Very bad topic title, but there is no quick way to
describe this problem in 2 words:

I am learning Ruby right now, and I am using the wonderful Learning R.
guide by Satish T… I am currently at the chapter “Random Numbers”
and I am trying to do the following:

This assignment is from Chris P.'s Book.

  1. Write a Deaf Grandma program. Whatever you say to grandma
    (whatever you type in), she should respond with HUH?! SPEAK UP, SONNY!,
    unless you shout it (type in all capitals). If you shout, she can hear
    you (or at least she thinks so) and yells back, NO, NOT SINCE 1938! To
    make your program really believable, have grandma shout a different year
    each time; maybe any year at random between 1930 and 1950. You can’t
    stop talking to grandma until you shout BYE.
  2. Extend your Deaf Grandma program: What if grandma doesn’t want you
    to leave? When you shout BYE, she could pretend not to hear you. Change
    your previous program so that you have to shout BYE three times in a
    row. Make sure to test your program: if you shout BYE three times, but
    not in a row, you should still be talking to grandma.

I have done all of that, but I have one problem. I am using SciTE, and
am using the F5 thing to try out my code. My one problem is that when I
run the program, the correct text comes on, but I have to press enter
twice for the while loop to start. Sounds confusing, I know, so here is
my code:

bye_response = 0
years = (1930…1950).to_a

puts “'Ey sonny! Long time no chat with your old nonna, eh? How ya
been!?”
STDOUT.flush

response = gets.chomp
while bye_response != 3
response = gets.chomp
if bye_response == 2
puts “FINE! LEAVE YOUR OLD NONNA!”
bye_response += 1
elsif response == “BYE”
puts “HUH!? WHAT WAS THAT!?”
bye_response += 1
elsif response == response.upcase
puts "NO, NOT SINCE " + years[rand(years.max - years.min)].to_s +
“!”
bye_response = 0
else
puts “HUH?! SPEAK UP, SONNY!”
bye_response = 0
end
STDOUT.flush
end

Try it out in SciTE and you’ll see what I mean. If you don’t have SciTE
and still want to see what I mean, I took a screenshot:
http://www.michaelboutros.com/uploads/ruby.jpg. Thanks a lot!

Sorry! I didn’t post the updated code:

bye_response = 0
years = (1930…1950).to_a

puts “'Ey sonny! Long time no chat with your old nonna, eh? How ya
been!?”
STDOUT.flush

response = gets.chomp

while bye_response != 3
response = gets.chomp
if response == “BYE”
if bye_response == 2
puts “FINE! LEAVE YOUR OLD NONNA!”
else
puts “HUH!? WHAT WAS THAT!?”
end
bye_response += 1
elsif response == response.upcase
puts "NO, NOT SINCE " + years[rand(years.max - years.min)].to_s +
“!”
bye_response = 0
else
puts “HUH?! SPEAK UP, SONNY!”
bye_response = 0
end
STDOUT.flush
end

Michael B. [email protected] writes:

I have done all of that, but I have one problem. I am using SciTE, and
am using the F5 thing to try out my code. My one problem is that when I
run the program, the correct text comes on, but I have to press enter
twice for the while loop to start.

No, you have to press enter once for the while loop to start, and
another time in the while loop:

bye_response = 0
years = (1930…1950).to_a

puts “'Ey sonny! Long time no chat with your old nonna, eh? How ya
been!?”
STDOUT.flush

response = gets.chomp

Here you have demanded the first enter.

while bye_response != 3
response = gets.chomp

And here you have demanded the second one, before even doing anything
with the previous response.

I suggest that you remove the first of those lines asking for input:
it does not actually get used anywhere.

David K. wrote:

I suggest that you remove the first of those lines asking for input:
it does not actually get used anywhere.

Ah, that makes sense. Thanks a lot for your help David :slight_smile: