Ruby - 循环



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

Ruby while 语句

语法

while conditional [do]
   code
end

条件为真时执行代码while循环的条件代码之间用保留字 do、换行符或分号 ; 分隔。

示例

#!/usr/bin/ruby

$i = 0
$num = 5

while $i < $num  do
   puts("Inside the loop i = #$i" )
   $i +=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

Ruby while 修饰符

语法

code while condition

OR

begin 
  code 
end while conditional

条件为真时执行代码

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

示例

#!/usr/bin/ruby

$i = 0
$num = 5
begin
   puts("Inside the loop i = #$i" )
   $i +=1
end while $i < $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 until 语句

until conditional [do]
   code
end

条件为假时执行代码until语句的条件与代码之间用保留字do、换行符或分号分隔。

示例

#!/usr/bin/ruby

$i = 0
$num = 5

until $i > $num  do
   puts("Inside the loop i = #$i" )
   $i +=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 until 修饰符

语法

code until conditional

OR

begin
   code
end until conditional

条件为假时执行代码

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

示例

#!/usr/bin/ruby

$i = 0
$num = 5
begin
   puts("Inside the loop i = #$i" )
   $i +=1;
end until $i > $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 语句

语法

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。语句 for i in 0..5 将允许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 语句

语法

redo

重新启动最内层循环的本次迭代,无需检查循环条件。如果在代码块内调用,则重新启动yieldcall

示例

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

语法

retry

如果retry出现在begin表达式的rescue子句中,则从begin语句体的开头重新启动。

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

如果retry出现在迭代器、代码块或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
............................
广告
© . All rights reserved.