Ruby中的循环用于执行指定次数的相同代码块.本章详细介绍了Ruby支持的所有循环语句.
while conditional [do] code end
执行代码而条件是真的. while 循环的条件通过保留字do,换行符,反斜杠\或分号分隔 code ;.
#!/usr/bin/ruby $i = 0 $num = 5 while $i < $num do puts("Inside the loop i = #$i" ) $i +=1 end
这将产生以下结果 :
Inside the loop i = 1 Inside the loop i = 2 Inside the loop i = 3 Inside the loop i = 4
code while condition OR begin code end while conditional
执行代码而有条件是真的.
如果 while 修饰符跟在开始语句后面没有救援或者确保子句,代码在评估条件之前执行一次.
#!/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
until conditional [do] code end
执行代码,而条件为false. 直到语句的条件通过保留字 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
code until conditional OR begin code end until conditional
当条件为假时,执行代码.
如果直到修饰符跟随开始语句而没有 rescue 或确保子句,代码在条件被评估.
#!/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
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
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
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
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 ............................
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 ............................