Newbie question

Sorry because this is probably lame but I’ve been trying to think this
one through for a few hours and after a few iterations can’t figure
out the answer. It’s an exercise in the book “Learn to Program”
(Pragmatic)

Anyway, the problem is I want the program to loop and exit on the input
‘BYE’
What’s I’ve come up with is:

$stdout.sync = true (due to weird windows buffer stuff)

input = ‘’

while input != ‘BYE’
input = gets.chomp

if input == input.upcase
puts ‘NO, NOT SINCE 1938!’
elsif input == input.downcase
puts ‘HUH?! SPEAK UP, SONNY!’

end
end

What happens is it does exit on BYE but issues the elsif before
ending. I’ve tried doing a
if input ==input.upcase !BYE , but was thrown an error. I know UNTIL
exists but it hasn’t been introduced in the book yet so want to work
with what I have.

tia
Stuart

What happens is it does exit on BYE but issues the elsif before
ending.

You could initialise input before you go into the while loop, and then
update it at the end of each iteration:

input = gets.chomp
while input != ‘BYE’
if input == input.upcase
puts ‘NO, NOT SINCE 1938!’
elsif input == input.downcase
puts ‘HUH?! SPEAK UP, SONNY!’
end
input = gets.chomp
end

See?

James

Yes, thank you James. Works as expected. In some of the examples in
the book “input” is initialized with a string. i.e.

request = ’ Compared to Go, Chess is like Tic-Tac-Toe.’
while request != ’ stop’
puts ’ What would you like to ask C to do?’
request = gets.chomp

Which is what threw me.

Stuart