Ruby Loops
Loops in Ruby are used to execute the same block of code a number of times. This chapter will describe in detail all the loop statements supported by Ruby.
Ruby while Statement
Syntax
while
conditional
[
do
]
code
end
or
Syntax
while
conditional
[
:
]
code
end
When conditional when is true, execute code。
In the syntax, do or : can be omitted. But if you write the while form on a single line, you must separate the condition or code block with do or :.
Example
!/usr/bin/ruby
-- coding: UTF-8 --
$i
=
0
$num
=
5
while
$i
<
$num
do
puts
(
"
In the loop statement i = #$i
"
)
$i
+=
1
end
Try it »
The output of the above example is:
在循环语句中 i = 0
在循环语句中 i = 1
在循环语句中 i = 2
在循环语句中 i = 3
在循环语句中 i = 4
Ruby while Modifiers
Syntax
code
while
condition
or
begin
code
end
while
conditional
When conditional when is true, execute code。
If both the while modifier follows a rescue statement without a begin or ensure clause,code will be called when the conditional executed once before the judgment.
Example
!/usr/bin/ruby
-- coding: UTF-8 --
$i
=
0
$num
=
5
begin
puts
(
"
In the loop statement i = #$i
"
)
$i
+=
1
end
while
$i
<
$num
Try it »
The output of the above example is:
在循环语句中 i = 0
在循环语句中 i = 1
在循环语句中 i = 2
在循环语句中 i = 3
在循环语句中 i = 4
Ruby until Statement
Syntax
until
conditional
[
do
]
code
end
When conditional when is false, execute code。
In the syntax, do can be omitted. But if you write the until form on a single line, you must separate the condition or code block with do.
Example
!/usr/bin/ruby
-- coding: UTF-8 --
$i
=
0
$num
=
5
until
$i
$num
do
puts
(
"
In the loop statement i = #$i
"
)
$i
+=
1
;
end
Try it »
The output of the above example is:
在循环语句中 i = 0
在循环语句中 i = 1
在循环语句中 i = 2
在循环语句中 i = 3
在循环语句中 i = 4
在循环语句中 i = 5
Ruby until Modifiers
Syntax
code
until
conditional
or
begin
code
end
until
conditional
When conditional when is false, execute code。
If both the until modifier follows a rescue statement without a begin or ensure clause,code will be called when the conditional executed once before the judgment.
Example
!/usr/bin/ruby
-- coding: UTF-8 --
$i
=
0
$num
=
5
begin
puts
(
"
In the loop statement i = #$i
"
)
$i
+=
1
;
end
until
$i
$num
Try it »
The output of the above example is:
在循环语句中 i = 0
在循环语句中 i = 1
在循环语句中 i = 2
在循环语句中 i = 3
在循环语句中 i = 4
在循环语句中 i = 5
Ruby for Statement
Syntax
for
variable
[
,
variable
...
]
in
expression
[
do
]
code
end
First evaluate the expression to obtain an object, then for expression execute once for each element in code。
Example
!/usr/bin/ruby
-- coding: UTF-8 --
for
i
in
.5
puts
"
The value of the local variable is #{i}
"
end
Try it »
Here, we have defined the range 0..5. The statement for i in 0..5 allows i to take values from 0 to 5 (inclusive).
The output of the above example is:
局部变量的值为 0
局部变量的值为 1
局部变量的值为 2
局部变量的值为 3
局部变量的值为 4
局部变量的值为 5
for...in The loop is almost completely equivalent to:
(
expression
)
.
each
do
|
variable
[
,
variable
...
]
|
code
end
However, the for loop does not create a new scope for local variables.
In the syntax, do can be omitted. But if you write the for form on a single line, you must separate the condition or code block with do.
Example
!/usr/bin/ruby
-- coding: UTF-8 --
(
.5
)
.
each
do
|
i
|
puts
"
The value of the local variable is #{i}
"
end
Try it »
The output of the above example is:
局部变量的值为 0
局部变量的值为 1
局部变量的值为 2
局部变量的值为 3
局部变量的值为 4
局部变量的值为 5
Ruby break Statement
Syntax
break
Terminates the innermost loop. If called within a block, terminates the method associated with the block (the method returns nil).
Example
!/usr/bin/ruby
-- coding: UTF-8 --
for
i
in
.5
if
i
2
then
break
end
puts
"
The value of the local variable is #{i}
"
end
Try it »
The output of the above example is:
局部变量的值为 0
局部变量的值为 1
局部变量的值为 2
Ruby next Statement
Syntax
next
Jumps to the next iteration of the loop. If called within a block, terminates the execution of the block (yield expression returns nil).
Example
!/usr/bin/ruby
-- coding: UTF-8 --
for
i
in
.5
if
i
<
2
then
next
end
puts
"
The value of the local variable is #{i}
"
end
Try it »
The output of the above example is:
局部变量的值为 2
局部变量的值为 3
局部变量的值为 4
局部变量的值为 5
Ruby redo Statement
Syntax
redo
Restarts the current iteration of the innermost loop without checking the loop condition. If called within a block, restarts yield or call。
Example
!/usr/bin/ruby
-- coding: UTF-8 --
for
i
in
.5
if
i
<
2
then
puts
"
The value of the local variable is #{i}
"
redo
end
end
This will produce the following result and will enter an infinite loop:
局部变量的值为 0
局部变量的值为 0
............................
Ruby retry Statement
Note: Versions 1.9 and later do not support using retry in loops.
Syntax
retry
If both the retry appears in the rescue clause of a begin expression, restarts from the beginning of the begin body.
begin
do_something
Exception raised
rescue
Handling errors
retry
Restart from begin
end
If retry appears within an iteration, a block, or the body of a for expression, it restarts the iteration call. The iteration's parameters are re-evaluated.
for
i
in
.5
retry
if
some_condition
Restart from i == 1
end
Example
!/usr/bin/ruby
-- coding: UTF-8 --
for
i
in
.5
retry
if
i
2
puts
"
The value of the local variable is #{i}
"
end
This will produce the following result and will enter an infinite loop:
局部变量的值为 1
局部变量的值为 2
局部变量的值为 1
局部变量的值为 2
局部变量的值为 1
局部变量的值为 2
............................