Loop while

Why can’t I do:

loop while t.gets !~ /[Cc]onnected/

Is there a similarly terse but readable way of doing this? I just want to
throw away all input until I reach the ‘connected’ line (assuming t is a
TCPSocket…)

Tim Bates

···


tim@bates.id.au

Tim Bates tim@bates.id.au writes:

Why can’t I do:

loop while t.gets !~ /[Cc]onnected/

If you want a one liner:

while t.gets !~ /[Cc]onnected/; end

or

until t.gets =~ /[Cc]onnected/; end

or

loop { break if t.gets =~ /[Cc]onnected/ }

Note that all of these will fall into an infinite loop at end of file
since both “nil =~ Regexp” and “Regexp =~ nil” are always false.

Is there a similarly terse but readable way of doing this? I just want to
throw away all input until I reach the ‘connected’ line (assuming t is a
TCPSocket…)

I would probably end up with:

while line = t.gets
  break if line =~ /[Cc]onnected/
end
fail "EOF waiting for Connected line" unless line

because I value robustness over terseness. :wink:

Hi,

Tim Bates tim@bates.id.au writes:

Why can’t I do:

loop while t.gets !~ /[Cc]onnected/

Is there a similarly terse but readable way of doing this? I just want to
throw away all input until I reach the ‘connected’ line (assuming t is a
TCPSocket…)

while t.gets !~ /[Cc]onnect/ do end
begin end while t.gets !~ /[Cc]onnect/
until t.gets =~ /[Cc]onnect/ do end
begin end until t.gets =~ /[Cc]onnect/
loop do break if t.gets =~ /[Cc]onnect/ end

If the string ‘connected’ is fixed, you can specify by the
argument of gets.

t.gets(“connected\n”)

···


eban