The loop control constructs
break
,
redo
, and
next
let you alter the normal flow through a loop or iterator.
break
terminates the immediately enclosing loop; control resumes
at the statement following the block.
redo
repeats the loop from
the start, but without reevaluating the condition or fetching the
next element (in an iterator).
next
skips to the end of the
loop, effectively starting the next iteration.
while gets
next if /^\s*#/ # skip comments
break if /^END/ # stop at end
# substitute stuff in backticks and try again
redo if gsub!(/`(.*?)`/) { eval($1) }
# process line ...
end
|
These keywords can also be used with any of the iterator-based
looping mechanisms:
i=0
loop do
i += 1
next if i < 3
print i
break if i > 4
end
|
produces: