Thank your for your time and attention and sorry for such a “noobie”
question!
WPPK
There is no best way, here are 2 ways to mull over, first way gives you
an idea
of how to break out of a loop. The 2ns way show use of a do/while loop,
that
test for the loop condition is done at the end, not the start.
Am Mittwoch, 14. Okt 2009, 11:55:50 +0900 schrieb Rajinder Y.:
Thomas W. wrote:
I have a loop running that needs to be broken when the user hits a
specified key.
There is no best way, here are 2 ways to mull over, first way gives you an
idea of how to break out of a loop. The 2ns way show use of a do/while
loop, that test for the loop condition is done at the end, not the start.
Be aware that pressing Ctrl-D (Unix) makes gets return nil, not a
string.
while(true)
c = gets
break if c.chomp == ‘x’
end
loop do
c = gets
break if not c or c.chomp == ‘x’
end
begin
c = gets.chomp
end while c != 'x
begin
c = gets.to_s.chomp
end while c != 'x
Bertram
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.