Ruby loop


Loops in Ruby are used to execute the same block of code several times. This chapter will provide a detailed introduction to all loop statements supported by Ruby.

Ruby while Statement

Syntax

while conditional [do]
   code
end

or

<pre>
while conditional [:]
   code
end

Execute when conditional is true code.

In the syntax, do or : can be omitted. But if you want to write the while expression in one line, you must separate the conditional expression or program block with do or :.

Example

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-

$i = 0
$num = 5

while $i < $num  do
   puts("在循环语句中 i = #$i" )
   $i +=1
end


The output result of the above example is:

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

Ruby while Modifier

Syntax

code while condition

或者

begin 
  code 
end while conditional

When conditional is true, code is executed.

If the while modifier follows a begin statement without a rescue or ensure clause, code will Execute once before conditional judgment.

Example

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-

$i = 0
$num = 5
begin
   puts("在循环语句中 i = #$i" )
   $i +=1
end while $i < $num


The output result of the above example is:

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

Ruby until Statement

until conditional [do]
   code
end

When conditional is false, execute code.

In the syntax, do can be omitted. But if you want to write the until formula on one line, you must separate the conditional expression or program block with do.

Example

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-

$i = 0
$num = 5

until $i > $num  do
   puts("在循环语句中 i = #$i" )
   $i +=1;
end


The output result of the above example is:

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

Ruby until Modifier

Syntax

code until conditional

或者

begin
   code
end until conditional

When conditional is false, code is executed.

If the until modifier follows a begin statement without a rescue or ensure clause, code will Execute once before conditional judgment.

Example

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-

$i = 0
$num = 5
begin
   puts("在循环语句中 i = #$i" )
   $i +=1;
end until $i > $num


The output result 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 calculate the expression to get an object, and then execute code once for each element in expression.

Example

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-

for i in 0..5
   puts "局部变量的值为 #{i}"
end

Here, we have defined the range 0..5. The statement for i in 0..5 allows values ​​of i from 0 to 5, inclusive.


The output result 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 want to write the for formula on one line, you must separate the conditional expression or program block with do.

Example

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-

(0..5).each do |i|
   puts "局部变量的值为 #{i}"
end


The output result of the above example is:

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

Ruby break Statement

Syntax

break

Terminate the innermost loop. If called within a block, terminates the method of the relevant block (method returns nil).

Example

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-

for i in 0..5
   if i > 2 then
      break
   end
   puts "局部变量的值为 #{i}"
end


The output result of the above example is:

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

Ruby next Statement

Syntax

next

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

Example

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-

for i in 0..5
   if i < 2 then
      next
   end
   puts "局部变量的值为 #{i}"
end


The output result of the above example is:

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

Ruby redo Statement

Syntax

redo

Restarts this iteration of the innermost loop without checking the loop condition. If called within a block, restart yield or call.

Example

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-

for i in 0..5
   if i < 2 then
      puts "局部变量的值为 #{i}"
      redo
   end
end

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

局部变量的值为 0
局部变量的值为 0
............................

Ruby retry Statement

Note: Versions 1.9 and later do not support the use of retry in loops.

Syntax

retry

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

begin
   do_something   # 抛出的异常
rescue
   # 处理错误
   retry          # 重新从 begin 开始
end

If retry appears within an iteration, within a block, or within the body of a for expression, restart the iteration call. Iteration parameters are re-evaluated.

for i in 1..5
   retry if some_condition # 重新从 i == 1 开始
end

Example

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-

for i in 1..5
   retry if  i > 2
   puts "局部变量的值为 #{i}"
end

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

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