Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
Kernel#loop
loop { block }
loop → an_enumerator
kernel.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.3.0/kernel.rb, line 180
def loop
unless block_given?
return enum_for(:loop) { Float::INFINITY }
end
begin
while true
yield
end
rescue StopIteration => e
e.result
end
end
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