With certain constructs, I have the choice of using a semicolon or the do keyword to delimit the end of a statement, as with the until example below.
until x == 100 do puts x; x+=1 end until x == 100; puts x; x+=1 end
But this is not possible with Kernel.loop.
x=0 loop do puts x; x+=1; break if x == 100 end x=0 loop; puts x; x+=1; break if x == 100 end # => error
Is there a reason why it’s like this?
loopis a method (inKernel) that really requires a block withdo...endor{ }.whileanduntilare statements (likeif), and do not expect a block. Thedokeyword is optional, but it does not denote a block (e.g.while x == 100 { puts x; x+=1; }will fail miserably, whereasloop { puts x; x+=1; break if x == 100 }will work just fine.)So,
domeans different things in the two cases. In the case ofloop(and other methods) it really denotes a block; in the case ofwhile,untiletc. it is just syntactic sugar (just likethenis afterif.) Do not be misled.