Ruby中的循环用于执行相同的代码块指定次数。本章详细介绍了Ruby支持的所有循环语句。
while conditional [do] code end
执行代码,而条件是真实的。甲而循环的条件是从分离代码由保留字做,换行,反斜线或分号。
#!/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
code while condition OR begin code end while conditional
执行代码,而条件是真实的。
如果while修饰符遵循没有拯救或确认子句的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
until conditional [do] code end
执行代码,而条件是假的。一到语句的条件是分开的代码被保留字做,换行或分号。
#!/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
执行代码,而条件是假的。
如果直到修饰符遵循没有拯救或确认子句的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
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中的声明将允许我从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
A for ... in循环几乎完全相当于以下 -
(expression).each do |variable[, variable...]| code end
除了for循环不会为局部变量创建新的范围。甲用于循环的表达从分离代码由保留字做,一个新行,或分号。
#!/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
跳转到最内部循环的下一个迭代。如果在块内调用(终止收益或调用返回零),则终止块的执行。
#!/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
重新启动最内部循环的此迭代,而不检查循环条件。如果在块内调用,重新启动产量或调用。
#!/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
如果retry出现在begin表达式的rescue子句中,请从begin body的开头重新启动。
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 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 ............................