English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Ruby Loops

Loops in Ruby are used to execute the same code block multiple times. This chapter will introduce all the loop statements supported by Ruby in detail.

Ruby while statement

syntax

while conditional [do]
   code
end

code until conditional

syntax

while conditional [:]
   code
end

end until conditional conditional execute when true code.

In the syntax, 'do' or ':' can be omitted. However, if you want to write a 'while' statement on one line, it must be separated by 'do' or ':' from the condition or code block.

Online Examples

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

The output of the above example is as follows:

in the loop statement, i = 0
in the loop statement, i = 1
in the loop statement, i = 2
in the loop statement, i = 3
in the loop statement, i = 4

Ruby while until $i > $num do

syntax

code while condition
code until conditional
begin 
  code 
end while conditional

end until conditional conditional execute when true code.

if while until rescue modifier follows a modifier without begin or the ensure clause'scode follows the conditional judges once before execution.

Online Examples

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

The output of the above example is as follows:

in the loop statement, i = 0
in the loop statement, i = 1
in the loop statement, i = 2
in the loop statement, i = 3
in the loop statement, i = 4

Ruby when it is false, execute statement

syntax

until conditional [do]
   code
end

end until conditional conditional 为假时,执行 code.

when it is false, execute

Online Examples

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
$i = 0
$num = 5
In the syntax, do can be omitted. However, if you want to write the until form on one line, you must separate the conditional expression or code block with do.
   puts("In the loop statement, i = #$i")
   $i +=1;
end

The output of the above example is as follows:

in the loop statement, i = 0
in the loop statement, i = 1
in the loop statement, i = 2
in the loop statement, i = 3
in the loop statement, i = 4
in the loop statement, i = 5

Ruby when it is false, execute until $i > $num do

syntax

modifier
code until conditional
begin
   code
or

end until conditional conditional when code.

if when it is false, execute until rescue modifier follows a modifier without begin or the ensure clause'scode follows the conditional judges once before execution.

Online Examples

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

The output of the above example is as follows:

in the loop statement, i = 0
in the loop statement, i = 1
in the loop statement, i = 2
in the loop statement, i = 3
in the loop statement, i = 4
in the loop statement, i = 5

Ruby for statement

syntax

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

first calculates the expression to get an object, and then processes expression each element of which is executed once code.

Online Examples

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

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 as follows:

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 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. However, if you want to write the for form on one line, you must separate the conditional expression or code block with do.

Online Examples

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

The output of the above example is as follows:

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

Ruby break statement

syntax

break

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

Online Examples

#!/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

The output of the above example is as follows:

the value of the local variable is 0
The value of the local variable is 1
The value of the local variable is 2

Ruby next statement

syntax

next

Jumps to the next iteration of the loop. If called within a block, it terminates the execution of the block (yield expression returns nil).

Online Examples

#!/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

The output of the above example is as follows:

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

Ruby redo statement

syntax

redo

restarts the current iteration of the innermost loop without checking the loop condition. If called within a block, it restarts. yield or call.

Online Examples

#!/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

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

the value of the local variable is 0
the value of the local variable is 0
............................

Ruby retry statement

Note:1.9and later versions do not support using retry within loops.

syntax

retry

if retry If it appears in the rescue clause of the begin expression, it will restart from the beginning of the begin body.

begin
   do_something   # Raised exception
rescue
   # Error Handling
   retry          # Resume from begin
end

If retry appears within an iteration, block, or the body of a for expression, the iteration will restart. The iteration parameters will be re-evaluated.

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

Online Examples

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
for i in 1..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:

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
............................