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

while conditional [do] code end

or

Syntax

while conditional [:] code end

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

#!/usr/bin/ruby # -*- coding: UTF-8 -*- $i = 0 $num = 5 while $i < $num do puts("#!/usr/bin/ruby # -*- coding: UTF-8 -*- $i = 0 $num = 5 while $i < $num do puts("In the loop statement i = #$i" ) $i +=1 end" ) $i +=1 end

Try it »

The output of the above example is:

在循环语句中 i = 0
在循环语句中 i = 1
在循环语句中 i = 2
在循环语句中 i = 3
在循环语句中 i = 4

Ruby whileModifier

Syntax

code while conditionorbegin code end while conditional

WhenconditionalWhen true, executecode。

IfwhileA modifier follows a ... withoutrescueor an ensure clausebeginafter a statement,codewill beconditionalexecuted once before the condition is evaluated.

Example

#!/usr/bin/ruby # -*- coding: UTF-8 -*- $i = 0 $num = 5 begin puts("#!/usr/bin/ruby # -*- coding: UTF-8 -*- $i = 0 $num = 5 begin puts("In the loop statement i = #$i" ) $i +=1 end while $i < $num" ) $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 untilStatement

Syntax

until conditional [do] code end

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

#!/usr/bin/ruby # -*- coding: UTF-8 -*- $i = 0 $num = 5 until $i > $num do puts("#!/usr/bin/ruby # -*- coding: UTF-8 -*- $i = 0 $num = 5 until $i > $num do puts("In the loop statement i = #$i" ) $i +=1; end" ) $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 untilModifier

Syntax

code until conditionalorbegin code end until conditional

WhenconditionalWhen false, executecode。

IfuntilA modifier follows a ... withoutrescueor an ensure clausebeginafter a statement,codewill beconditionalexecuted once before the condition is evaluated.

Example

#!/usr/bin/ruby # -*- coding: UTF-8 -*- $i = 0 $num = 5 begin puts("#!/usr/bin/ruby # -*- coding: UTF-8 -*- $i = 0 $num = 5 begin puts("In the loop statement i = #$i" ) $i +=1; end until $i > $num" ) $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 forStatement

Syntax

for variable [, variable ...] in expression [do] code end

First evaluate the expression to obtain an object, then forexpressioneach element in it, execute oncecode。

Example

#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 0..5 puts "#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 0..5 puts "The value of the local variable is #{i}" end" end

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:

(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 want to write a for expression in one line, you must use do to separate the condition from the code block.

Example

#!/usr/bin/ruby # -*- coding: UTF-8 -*- (0..5).each do |i| puts "#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 0..5 do puts "The value of the local variable is #{i}" end" end

Try it »

The output of the above example is:

局部变量的值为 0
局部变量的值为 1
局部变量的值为 2
局部变量的值为 3
局部变量的值为 4
局部变量的值为 5

Ruby breakStatement

Syntax

break

Terminates the innermost loop. If called within a block, it terminates the method associated with the block (the method returns nil).

Example

#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 0..5 if i > 2 then break end puts "#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 0..5 if i > 2 then break end puts "The value of the local variable is #{i}" end" end

Try it »

The output of the above example is:

局部变量的值为 0
局部变量的值为 1
局部变量的值为 2

Ruby nextStatement

Syntax

next

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

#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 0..5 if i < 2 then next end puts "#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 0..5 if i < 2 then next end puts "The value of the local variable is #{i}" end" end

Try it »

The output of the above example is:

局部变量的值为 2
局部变量的值为 3
局部变量的值为 4
局部变量的值为 5

Ruby redoStatement

Syntax

redo

Restarts the current iteration of the innermost loop, without checking the loop condition. If called within a block, it restartsyieldorcall。

Example

#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 0..5 if i < 2 then puts "#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 0..5 if i < 2 then puts "The value of the local variable is #{i}" redo end end" redo end end

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

retry

Ifretryappears in the rescue clause of a begin expression, then restarts from the beginning of the begin body.

begin do_something #the raised exception rescue #handle errors retry #restart from the beginning of begin end

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.

for i in 1..5 retry if some_condition #restart from i == 1 end

Example

#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 1..5 retry if i > 2 puts "#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 0..5 retry if i > 2 puts "The value of the local variable is #{i}" end" end

This will produce the following result, and will enter an infinite loop:

局部变量的值为 1
局部变量的值为 2
局部变量的值为 1
局部变量的值为 2
局部变量的值为 1
局部变量的值为 2
............................
Other Extensions