Method: Kernel#loop

Defined in:
vm_eval.c

#loop { ... } ⇒ Object #loopObject

Repeatedly executes the block.

If no block is given, an enumerator is returned instead.

loop do
  print "Input: "
  line = gets
  break if !line or line =~ /^qQ/
  # ...
end

StopIteration raised in the block breaks the loop. In this case, loop returns the “result” value stored in the exception.

enum = Enumerator.new { |y|
  y << "one"
  y << "two"
  :ok
}

result = loop {
  puts enum.next
} #=> :ok

Overloads:

  • #loop { ... } ⇒ Object

    Yields:



1477
1478
1479
1480
1481
1482
# File 'vm_eval.c', line 1477

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);
}