Ruby中的循环用于执行指定次数的相同代码块.本章详细介绍了Ruby支持的所有循环语句.
Ruby while Statement
语法
while conditional [do] codeend
执行代码而条件是真的. while 循环的条件通过保留字do,换行符,反斜杠\或分号分隔 code ;.
示例
#!/usr/bin/ruby$i = 0$num = 5while $i < $num do puts("Inside the loop i = #$i" ) $i +=1end
这将产生以下结果 :
Inside the loop i = 1Inside the loop i = 2Inside the loop i = 3Inside the loop i = 4
Ruby while修饰符
语法
code while conditionORbegin code end while conditional
执行代码而有条件是真的.
如果 while 修饰符跟在开始语句后面没有救援或者确保子句,代码在评估条件之前执行一次.
示例
#!/usr/bin/ruby$i = 0$num = 5begin puts("Inside the loop i = #$i" ) $i +=1end while $i < $num
这将产生以下结果 :
Inside the loop i = 0Inside the loop i = 1Inside the loop i = 2Inside the loop i = 3Inside the loop i = 4
Ruby直到Statement
until conditional [do] codeend
执行代码,而条件为false. 直到语句的条件通过保留字 do ,换行符或分号与代码分开.
示例
#!/usr/bin/ruby$i = 0$num = 5until $i > $num do puts("Inside the loop i = #$i" ) $i +=1;end
这将产生以下结果 :
Inside the loop i = 0Inside the loop i = 1Inside the loop i = 2Inside the loop i = 3Inside the loop i = 4Inside the loop i = 5
Ruby直到修饰符
语法
code until conditionalORbegin codeend until conditional
当条件为假时,执行代码.
如果直到修饰符跟随开始语句而没有 rescue 或确保子句,代码在条件被评估.
示例
#!/usr/bin/ruby$i = 0$num = 5begin puts("Inside the loop i = #$i" ) $i +=1;end until $i > $num
这将产生以下结果 :
Inside the loop i = 0Inside the loop i = 1Inside the loop i = 2Inside the loop i = 3Inside the loop i = 4Inside the loop i = 5
Ruby for Statement
语法
for variable [, variable ...] in expression [do] codeend
为表达式中的每个元素执行一次代码.
示例
#!/usr/bin/rubyfor 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 0Value of local variable is 1Value of local variable is 2Value of local variable is 3Value of local variable is 4Value 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 0Value of local variable is 1Value of local variable is 2Value of local variable is 3Value of local variable is 4Value of local variable is 5
Ruby break语句
语法
break
终止最多内循环.如果在块内调用,则终止具有关联块的方法(方法返回nil).
示例
#!/usr/bin/rubyfor i in 0..5 if i > 2 then break end puts "Value of local variable is #{i}"end
这将产生以下结果 :
Value of local variable is 0Value of local variable is 1Value of local variable is 2
Ruby next语句
语法
next
跳转到最内部循环的下一次迭代.如果在块内调用( yield 或调用返回nil),则终止块的执行.
示例
#!/usr/bin/rubyfor i in 0..5 if i < 2 then next end puts "Value of local variable is #{i}"end
这将产生以下结果 :
Value of local variable is 2Value of local variable is 3Value of local variable is 4Value of local variable is 5
Ruby redo Statement
语法
redo
重新启动最内部循环的迭代,而不检查循环条件.如果在块中调用,则重新启动 yield 或调用.
示例
#!/usr/bin/rubyfor i in 0..5 if i < 2 then puts "Value of local variable is #{i}" redo endend
这将产生以下结果并将进入无限循环 :
Value of local variable is 0Value of local variable is 0............................
Ruby重试语句
语法
retry
如果重试出现在begin表达式的rescue子句中,请从begin体的开头重新开始.
begin do_something # exception raisedrescue # handles error retry # restart from beginningend
如果重试出现在迭代器,块或 for 表达式的主体中,则重新启动迭代器调用的调用.迭代器的参数被重新评估.
for i in 1..5 retry if some_condition # restart from i == 1end
示例
#!/usr/bin/rubyfor i in 0..5 retry if i > 2puts "Value of local variable is #{i}"end
这将产生以下结果并将进入无限循环 :
Value of local variable is 1Value of local variable is 2Value of local variable is 1Value of local variable is 2Value of local variable is 1Value of local variable is 2............................