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
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.
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.
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.
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
Ruby next Statement
Ruby redo Statement
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
............................