Ruby Loops
Loops in Ruby are used to execute the same block of code a number of times. This chapter will detail all the loop statements supported by Ruby.
Ruby whileStatement
Syntax
or
Syntax
WhenconditionalWhen true, executecode。
In the syntax, do or : can be omitted. But if you want to write a while expression in one line, you must use do or : to separate the condition from the code block.
Example
Try it »
The output of the above example is:
在循环语句中 i = 0 在循环语句中 i = 1 在循环语句中 i = 2 在循环语句中 i = 3 在循环语句中 i = 4
Ruby whileModifier
Syntax
WhenconditionalWhen true, executecode。
IfwhileA modifier follows a ... withoutrescueor an ensure clausebeginafter a statement,codewill beconditionalexecuted once before the condition is evaluated.
Example
Try it »
The output of the above example is:
在循环语句中 i = 0 在循环语句中 i = 1 在循环语句中 i = 2 在循环语句中 i = 3 在循环语句中 i = 4
Ruby untilStatement
Syntax
WhenconditionalWhen false, executecode。
In the syntax, do can be omitted. But if you want to write an until expression in one line, you must use do to separate the condition from the code block.
Example
Try it »
The output of the above example is:
在循环语句中 i = 0 在循环语句中 i = 1 在循环语句中 i = 2 在循环语句中 i = 3 在循环语句中 i = 4 在循环语句中 i = 5
Ruby untilModifier
Syntax
WhenconditionalWhen false, executecode。
IfuntilA modifier follows a ... withoutrescueor an ensure clausebeginafter a statement,codewill beconditionalexecuted once before the condition is evaluated.
Example
Try it »
The output of the above example is:
在循环语句中 i = 0 在循环语句中 i = 1 在循环语句中 i = 2 在循环语句中 i = 3 在循环语句中 i = 4 在循环语句中 i = 5
Ruby forStatement
Syntax
First evaluate the expression to obtain an object, then forexpressioneach element in it, execute oncecode。
Example
Try it »
Here, we have defined the range 0..5. The statement for i in 0..5 allows the value of i to range from 0 to 5 (including 5).
The output of the above example is:
局部变量的值为 0 局部变量的值为 1 局部变量的值为 2 局部变量的值为 3 局部变量的值为 4 局部变量的值为 5
for...inThe loop is almost exactly equivalent to:
However, the for loop does not create a new scope for local variables.
In the syntax, do can be omitted. But if you want to write a for expression in one line, you must use do to separate the condition from the code block.
Example
Try it »
The output of the above example is:
局部变量的值为 0 局部变量的值为 1 局部变量的值为 2 局部变量的值为 3 局部变量的值为 4 局部变量的值为 5
Ruby breakStatement
Syntax
Terminates the innermost loop. If called within a block, it terminates the method associated with the block (the method returns nil).
Example
Try it »
The output of the above example is:
局部变量的值为 0 局部变量的值为 1 局部变量的值为 2
Ruby nextStatement
Syntax
Jumps to the next iteration of the loop. If called within a block, it terminates the execution of the block (yieldthe expression returns nil).
Example
Try it »
The output of the above example is:
局部变量的值为 2 局部变量的值为 3 局部变量的值为 4 局部变量的值为 5
Ruby redoStatement
Syntax
Restarts the current iteration of the innermost loop, without checking the loop condition. If called within a block, it restartsyieldorcall。
Example
This will produce the following result, and will enter an infinite loop:
局部变量的值为 0 局部变量的值为 0 ............................
Ruby retryStatement
Note: Versions 1.9 and later do not support using retry in loops.
Syntax
Ifretryappears in the rescue clause of a begin expression, then restarts from the beginning of the begin body.
If retry appears inside an iteration, a block, or the body of a for expression, it restarts the iteration call. The arguments of the iteration are re-evaluated.
Example
This will produce the following result, and will enter an infinite loop:
局部变量的值为 1 局部变量的值为 2 局部变量的值为 1 局部变量的值为 2 局部变量的值为 1 局部变量的值为 2 ............................Other Extensions