Ruby 简明教程

Ruby - Loops

Ruby 中的循环用于按指定次数执行相同的代码块。本教程详细说明 Ruby 支持的所有循环语句。

Loops in Ruby are used to execute the same block of code a specified number of times. This chapter details all the loop statements supported by Ruby.

Ruby while Statement

Syntax

while conditional [do]
   code
end

执行代码,当条件为真时。while 循环的条件由保留字 do、换行符、反斜杠 \ 或分号 ; 与代码分开。

Executes code while conditional is true. A while loop’s conditional is separated from code by the reserved word do, a newline, backslash \, or a semicolon ;.

Example

#!/usr/bin/ruby

$i = 0
$num = 5

while $i < $num  do
   puts("Inside the loop i = #$i" )
   $i +=1
end

这会产生以下结果 −

This will produce the following result −

Inside the loop i = 0
Inside the loop i = 1
Inside the loop i = 2
Inside the loop i = 3
Inside the loop i = 4

Ruby while modifier

Syntax

code while condition

OR

begin
  code
end while conditional

执行代码,当条件为真时。

Executes code while conditional is true.

如果 while 修饰符后跟没有 rescue 或 ensure 子句的 begin 语句,则会在评估条件之前执行一次代码。

If a while modifier follows a begin statement with no rescue or ensure clauses, code is executed once before conditional is evaluated.

Example

#!/usr/bin/ruby

$i = 0
$num = 5
begin
   puts("Inside the loop i = #$i" )
   $i +=1
end while $i < $num

这会产生以下结果 −

This will produce the following result −

Inside the loop i = 0
Inside the loop i = 1
Inside the loop i = 2
Inside the loop i = 3
Inside the loop i = 4

Ruby until Statement

until conditional [do]
   code
end

执行代码,当条件为假时。until 语句的条件由保留字 do、换行符或分号与代码分开。

Executes code while conditional is false. An until statement’s conditional is separated from code by the reserved word do, a newline, or a semicolon.

Example

#!/usr/bin/ruby

$i = 0
$num = 5

until $i > $num  do
   puts("Inside the loop i = #$i" )
   $i +=1;
end

这会产生以下结果 −

This will produce the following result −

Inside the loop i = 0
Inside the loop i = 1
Inside the loop i = 2
Inside the loop i = 3
Inside the loop i = 4
Inside the loop i = 5

Ruby until modifier

Syntax

code until conditional

OR

begin
   code
end until conditional

执行代码,当条件为假时。

Executes code while conditional is false.

如果 until 修饰符后跟没有 rescue 或 ensure 子句的 begin 语句,则会在评估条件之前执行一次代码。

If an until modifier follows a begin statement with no rescue or ensure clauses, code is executed once before conditional is evaluated.

Example

#!/usr/bin/ruby

$i = 0
$num = 5
begin
   puts("Inside the loop i = #$i" )
   $i +=1;
end until $i > $num

这会产生以下结果 −

This will produce the following result −

Inside the loop i = 0
Inside the loop i = 1
Inside the loop i = 2
Inside the loop i = 3
Inside the loop i = 4
Inside the loop i = 5

Ruby for Statement

Syntax

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

针对表达式中的每个元素执行代码一次。

Executes code once for each element in expression.

Example

#!/usr/bin/ruby

for i in 0..5
   puts "Value of local variable is #{i}"
end

在这里,我们定义了范围 0..5。语句 for i in 0..5 将允许 i 取 0 到 5(包括 5)范围内的值。这将产生以下结果 −

Here, we have defined the range 0..5. The statement for i in 0..5 will allow i to take values in the range from 0 to 5 (including 5). This will produce the following result −

Value of local variable is 0
Value of local variable is 1
Value of local variable is 2
Value of local variable is 3
Value of local variable is 4
Value of local variable is 5

for…​in 循环与以下内容几乎完全等效 −

A for…​in loop is almost exactly equivalent to the following −

(expression).each do |variable[, variable...]| code end

但 for 循环不会为局部变量创建新作用域。for 循环的表达式由保留字 do、换行符或分号与代码分开。

except that a for loop doesn’t create a new scope for local variables. A for loop’s expression is separated from code by the reserved word do, a newline, or a semicolon.

Example

#!/usr/bin/ruby

(0..5).each do |i|
   puts "Value of local variable is #{i}"
end

这会产生以下结果 −

This will produce the following result −

Value of local variable is 0
Value of local variable is 1
Value of local variable is 2
Value of local variable is 3
Value of local variable is 4
Value of local variable is 5

Ruby break Statement

Syntax

break

终止最内部循环。如果在块内调用,则终止具有关联块的方法(方法返回 nil)。

Terminates the most internal loop. Terminates a method with an associated block if called within the block (with the method returning nil).

Example

#!/usr/bin/ruby

for i in 0..5
   if i > 2 then
      break
   end
   puts "Value of local variable is #{i}"
end

这会产生以下结果 −

This will produce the following result −

Value of local variable is 0
Value of local variable is 1
Value of local variable is 2

Ruby next Statement

Syntax

next

跳转到最内部循环的下一个迭代。如果在块内调用,则终止块的执行(yield 或调用返回 nil)。

Jumps to the next iteration of the most internal loop. Terminates execution of a block if called within a block (with yield or call returning nil).

Example

#!/usr/bin/ruby

for i in 0..5
   if i < 2 then
      next
   end
   puts "Value of local variable is #{i}"
end

这会产生以下结果 −

This will produce the following result −

Value of local variable is 2
Value of local variable is 3
Value of local variable is 4
Value of local variable is 5

Ruby redo Statement

Syntax

redo

重新启动最内部循环的此迭代,而不检查循环条件。如果在块中调用,则重新启动 yield 或调用。

Restarts this iteration of the most internal loop, without checking loop condition. Restarts yield or call if called within a block.

Example

#!/usr/bin/ruby

for i in 0..5
   if i < 2 then
      puts "Value of local variable is #{i}"
      redo
   end
end

这将产生以下结果,并进入无限循环 −

This will produce the following result and will go in an infinite loop −

Value of local variable is 0
Value of local variable is 0
............................

Ruby retry Statement

Syntax

retry

如果重试出现在 begin 表达式的救援子句中,则从 begin 体的开头重新开始。

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

begin
   do_something # exception raised
rescue
   # handles error
   retry  # restart from beginning
end

如果重试出现在迭代器、块或 for 表达式的体中,则会重新开始迭代器调用的调用。对迭代器的参数会重新求值。

If retry appears in the iterator, the block, or the body of the for expression, restarts the invocation of the iterator call. Arguments to the iterator is re-evaluated.

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

Example

#!/usr/bin/ruby
for i in 0..5
   retry if i > 2
puts "Value of local variable is #{i}"
end

这将产生以下结果,并进入无限循环 −

This will produce the following result and will go in an infinite loop −

Value of local variable is 1
Value of local variable is 2
Value of local variable is 1
Value of local variable is 2
Value of local variable is 1
Value of local variable is 2
............................