End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Kernel#loop
loop { block }
loop → an_enumerator
vm_eval.c
View on GitHub ↗
static VALUE
rb_f_loop(VALUE self)
{
RETURN_SIZED_ENUMERATOR(self, 0, 0, rb_f_loop_size);
return rb_rescue2(loop_i, (VALUE)0, loop_stop, (VALUE)0, rb_eStopIteration, (VALUE)0);
}
Repeatedly executes the block.
If no block is given, an enumerator is returned instead.
Example
loop do print "Input: " line = gets break if !line or line =~ /^q/i # ... end
StopIteration raised in the block breaks the loop. In this case, loop returns the “result” value stored in the exception.
Example
enum = Enumerator.new { |y| y << "one" y << "two" :ok } result = loop { puts enum.next } #=> :ok