Loops in Ruby are used to execute the same block of code several times. Thissection details all the loop statements supported by Ruby. Or When In grammar The output result of the above example is: When If The output result of the above example is: When In grammar The output result of the above example is: When conditional is false, execute code . If The output result of the above example is: First evaluate the expression to get an object, and then target the Here, we have defined the range 0. 5. The statement for i in 0.. 5 allows the value of I from 0 to 5 (inclusive). The output result of the above example is: However, the for loop does not create a new scope for local variables. In grammar The output result of the above example is: End the innermost loop. If called within a block, the method of the related block is terminated (the method returns The output result of the above example is: Skip to the next iteration of the loop. If called within a block, the execution of the block is terminated The output result of the above example is: Restart the iteration of the innermost loop without checking the loop conditions. If called within a block, start over This will produce the following results and enter an infinite loop: Note:1.9 and later versions do not support using If If This will produce the following results and enter an infinite loop: 6.17.1. Ruby
while
statement # Grammar #
whileconditional[do]codeend
Grammar #
whileconditional[:]codeend
conditional
is true, execute
code
.
do
or
:
can be omitted without writing. But if you want to write it in one line `` While`` type, you must use the
do
or
:
separate conditional or program blocks.Example #
#!/usr/bin/ruby#-*- coding: UTF-8
-*-$i=0$num=5while$i<$numdoputs("In a loop statement i = #$i")$i+=1end
In a loop statement, i=0
In a loop statement, i=1
In a loop statement, i=2
In a loop statement, i=3
In a loop statement, i=4
6.17.2. Ruby
while
modifier # Grammar #
codewhilecondition or begincodeendwhileconditional
conditional
is true, execute
code
.
while
modifier follows a no
rescue
or
ensure
of the clause
begin
after the statement
code
will be in
conditional
execute once before judgment.Example #
#!/usr/bin/ruby#-*- coding: UTF-8 -*-$i=0$num=5beginputs("In a loop statement i
= #$i")$i+=1endwhile$i<$num
In a loop statement, i=0
In a loop statement, i=1
In a loop statement, i=2
In a loop statement, i=3
In a loop statement, i=4
6.17.3. Ruby
until
statement # Grammar #
untilconditional[do]codeend
conditional
is false, execute
code
.
do
can omit it. But if you want to write a
until
type, you must use the
do
separate conditional or program blocks.Example #
#!/usr/bin/ruby#-*- coding: UTF-8
-*-$i=0$num=5until$i>$numdoputs("In a loop statement i = #$i")$i+=1;end
In a loop statement, i=0
In a loop statement, i=1
In a loop statement, i=2
In a loop statement, i=3
In a loop statement, i=4
In a loop statement, i=5
6.17.4. Ruby
until
modifier # Grammar #
codeuntilconditional or begincodeenduntilconditional
until
modifier follows a no
rescue
or
ensure
of the clause
begin
after the statement
code
will be in
conditional
execute once before judgment.Example #
#!/usr/bin/ruby#-*- coding: UTF-8 -*-$i=0$num=5beginputs("In a loop statement i
= #$i")$i+=1;enduntil$i>$num
In a loop statement, i=0
In a loop statement, i=1
In a loop statement, i=2
In a loop statement, i=3
In a loop statement, i=4
In a loop statement, i=5
6.17.5. Ruby
for
statement # Grammar #
forvariable[,variable...]inexpression[do]codeend
expression
execute each element in the
code
.Example #
#!/usr/bin/ruby#-*- coding: UTF-8 -*-foriin0..5puts"The value of a local variable is
#{i}"end
The value of the local variable is 0
The value of the local variable is 1
The value of the local variable is 2
The value of the local variable is 3
The value of the local variable is 4
The value of the local variable is 5
for...in
loop is almost exactly equivalent to:(expression).eachdo\|variable[,variable...]\|codeend
do
can omit it. But if you want to write a
for
type, you must use the
do
separate conditional or program blocks.Example #
#!/usr/bin/ruby#-*- coding: UTF-8
-*-(0..5).eachdo\|i\|puts"The value of a local variable is #{i}"end
The value of the local variable is 0
The value of the local variable is 1
The value of the local variable is 2
The value of the local variable is 3
The value of the local variable is 4
The value of the local variable is 5
6.17.6. Ruby
break
statement # Grammar #
break
nil
).Example #
#!/usr/bin/ruby#-*- coding: UTF-8
-*-foriin0..5ifi>2thenbreakendputs"The value of a local variable is #{i}"end
The value of the local variable is 0
The value of the local variable is 1
The value of the local variable is 2
6.17.7. Ruby
next
statement # Grammar #
next
yield
expression returns
nil
).Example #
#!/usr/bin/ruby#-*- coding: UTF-8
-*-foriin0..5ifi<2thennextendputs"The value of a local variable is #{i}"end
The value of the local variable is 2
The value of the local variable is 3
The value of the local variable is 4
The value of the local variable is 5
6.17.8. Ruby
redo
statement # Grammar #
redo
yield
or
call
.Example #
#!/usr/bin/ruby#-*- coding: UTF-8
-*-foriin0..5ifi<2thenputs"The value of a local variable is #{i}"redoendend
The value of the local variable is 0
The value of the local variable is 0
............................
6.17.9. Ruby
retry
statement #
retry
in loops.Grammar #
retry
retry
appear in
begin
of the expression
rescue
clause, then from the
begin
the beginning of the subject starts over.begindo_something#Exception thrownrescue
#process errorretry#Starting from begin againend
retry
appears within an iteration, within a block, or
for
within the body of the expression, the iterative call is restarted. The parameters of the iteration are reevaluated.foriin1..5retryifsome_condition#Starting from i==1 again end
Example #
#!/usr/bin/ruby#-*- coding: UTF-8
-*-foriin1..5retryifi>2puts"The value of a local variable is #{i}"end
The value of the local variable is 1
The value of the local variable is 2
The value of the local variable is 1
The value of the local variable is 2
The value of the local variable is 1
The value of the local variable is 2
............................