Ruby - 循环

Ruby中的循环用于执行指定次数的相同代码块.本章详细介绍了Ruby支持的所有循环语句.

Ruby while Statement

语法

while conditional [do]
   code
end


执行代码条件是真的. while 循环的条件通过保留字do,换行符,反斜杠\或分号分隔 code ;.

示例 

#!/usr/bin/ruby

$i = 0
$num = 5

while &dollar;i < &dollar;num  do
   puts("Inside the loop i = #&dollar;i" )
   &dollar;i &plus;=1
end

这将产生以下结果 :

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


Ruby while修饰符

语法

code while condition

OR

begin 
  code 
end while conditional


执行代码有条件是真的.

如果 while 修饰符跟在开始语句后面没有救援或者确保子句,代码在评估条件之前执行一次.

示例

#!/usr/bin/ruby

&dollar;i = 0
&dollar;num = 5
begin
   puts("Inside the loop i = #&dollar;i" )
   &dollar;i &plus;=1
end while &dollar;i < &dollar;num

这将产生以下结果 :

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直到Statement

until conditional [do]
   code
end


执行代码,而条件为false. 直到语句的条件通过保留字 do ,换行符或分号与代码分开.

示例

#!/usr/bin/ruby

&dollar;i = 0
&dollar;num = 5

until &dollar;i > &dollar;num  do
   puts("Inside the loop i = #&dollar;i" )
   &dollar;i &plus;=1;
end


这将产生以下结果 :

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直到修饰符

语法

code until conditional

OR

begin
   code
end until conditional


条件为假时,执行代码.

如果直到修饰符跟随开始语句而没有 rescue 或确保子句,代码条件被评估.

示例

#!/usr/bin/ruby

&dollar;i = 0
&dollar;num = 5
begin
   puts("Inside the loop i = #&dollar;i" )
   &dollar;i &plus;=1;
end until &dollar;i > &dollar;num

这将产生以下结果 :

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

语法

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


表达式中的每个元素执行一次代码.

示例

#!/usr/bin/ruby

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


在这里,我们定义了范围0..5.在0..5中 i 的陈述将允许 i 取0到5(包括5)范围内的值.这将产生以下结果 :

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 循环几乎完全等同于以下 :

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


除了 for 循环不为局部变量创建新范围. for 循环的表达式通过保留字do,换行符或分号与代码分开.

示例

#!/usr/bin/ruby

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


这将产生以下结果 :

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语句

语法

break


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

示例

#!/usr/bin/ruby

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

这将产生以下结果 :

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

Ruby next语句

语法

next


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

示例

#!/usr/bin/ruby

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

这将产生以下结果 :

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

语法

redo


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

示例

#!/usr/bin/ruby

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

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

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

Ruby重试语句

语法

retry


如果重试出现在begin表达式的rescue子句中,请从begin体的开头重新开始.

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


如果重试出现在迭代器,块或 for 表达式的主体中,则重新启动迭代器调用的调用.迭代器的参数被重新评估.

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

示例

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

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

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