详解Ruby中的循环语句的用法
Ruby中的循环用于执行相同的代码块若干次。本章节将详细介绍Ruby支持的所有循环语句。
Rubywhile语句
语法
whileconditional[do] code end
当conditional为真时,执行code。while循环的conditional通过保留字do、一个换行符、反斜线\或一个分号;,来与code分离开。
实例
#!/usr/bin/ruby $i=0 $num=5 while$i<$numdo puts("Insidetheloopi=#$i") $i+=1 end
这将产生以下结果:
Insidetheloopi=0 Insidetheloopi=1 Insidetheloopi=2 Insidetheloopi=3 Insidetheloopi=4
Rubywhile修饰符
语法
codewhilecondition OR begin code endwhileconditional
当conditional为真时,执行code。
如果while修饰符跟在一个没有rescue或ensure子句的begin语句后面,code会在conditional判断之前执行一次。
实例
#!/usr/bin/ruby $i=0 $num=5 begin puts("Insidetheloopi=#$i") $i+=1 endwhile$i<$num
这将产生以下结果:
Insidetheloopi=0 Insidetheloopi=1 Insidetheloopi=2 Insidetheloopi=3 Insidetheloopi=4 Rubyuntil语句 untilconditional[do] code end
当conditional为假时,执行code。until语句的conditional通过保留字do、一个换行符或一个分号,来与code分离开。
实例
#!/usr/bin/ruby $i=0 $num=5 until$i>$numdo puts("Insidetheloopi=#$i") $i+=1; end
这将产生以下结果:
Insidetheloopi=0 Insidetheloopi=1 Insidetheloopi=2 Insidetheloopi=3 Insidetheloopi=4 Insidetheloopi=5 Rubyuntil修饰符 语法 codeuntilconditional OR begin code enduntilconditional
当conditional为假时,执行code。
如果until修饰符跟在一个没有rescue或ensure子句的begin语句后面,code会在conditional判断之前执行一次。
实例
#!/usr/bin/ruby $i=0 $num=5 begin puts("Insidetheloopi=#$i") $i+=1; enduntil$i>$num
这将产生以下结果:
Insidetheloopi=0 Insidetheloopi=1 Insidetheloopi=2 Insidetheloopi=3 Insidetheloopi=4 Insidetheloopi=5
Rubyfor语句
语法
forvariable[,variable...]inexpression[do] code end
针对expression中的每个元素分别执行一次code。
实例
#!/usr/bin/ruby foriin0..5 puts"Valueoflocalvariableis#{i}" end
在这里,我们已经定义了范围0..5。语句foriin0..5允许i的值从0到5(包含5)。这将产生以下结果:
Valueoflocalvariableis0 Valueoflocalvariableis1 Valueoflocalvariableis2 Valueoflocalvariableis3 Valueoflocalvariableis4 Valueoflocalvariableis5
for...in循环几乎是完全等价于:
(expression).eachdo|variable[,variable...]|codeend
但是,for循环不会为局部变量创建一个新的作用域。for循环的expression通过保留字do、一个换行符或一个分号,来与code分离开。.
实例
#!/usr/bin/ruby (0..5).eachdo|i| puts"Valueoflocalvariableis#{i}" end
这将产生以下结果:
Valueoflocalvariableis0 Valueoflocalvariableis1 Valueoflocalvariableis2 Valueoflocalvariableis3 Valueoflocalvariableis4 Valueoflocalvariableis5
Rubybreak语句
语法
break
终止最内部的循环。如果在块内调用,则终止相关块的方法(方法返回nil)。
实例
#!/usr/bin/ruby foriin0..5 ifi>2then break end puts"Valueoflocalvariableis#{i}" end
这将产生以下结果:
Valueoflocalvariableis0 Valueoflocalvariableis1 Valueoflocalvariableis2
Rubynext语句
语法
next
跳到最内部循环的下一个迭代。如果在块内调用,则终止块的执行(yield或调用返回nil)。
实例
#!/usr/bin/ruby foriin0..5 ifi<2then next end puts"Valueoflocalvariableis#{i}" end
这将产生以下结果:
Valueoflocalvariableis2 Valueoflocalvariableis3 Valueoflocalvariableis4 Valueoflocalvariableis5
Rubyredo语句
语法
redo
重新开始最内部循环的该次迭代,不检查循环条件。如果在块内调用,则重新开始yield或call。
实例
#!/usr/bin/ruby foriin0..5 ifi<2then puts"Valueoflocalvariableis#{i}" redo end end
这将产生以下结果,并会进入一个无限循环:
Valueoflocalvariableis0 Valueoflocalvariableis0 ............................
Rubyretry语句
语法
retry
如果retry出现在begin表达式的rescue子句中,则从begin主体的开头重新开始。
begin do_something#抛出的异常 rescue #处理错误 retry#重新从begin开始 end
如果retry出现在迭代内、块内或者for表达式的主体内,则重新开始迭代调用。迭代的参数会重新评估。
foriin1..5 retryifsome_condition#重新从i==1开始 end
实例
#!/usr/bin/ruby foriin1..5 retryifi>2 puts"Valueoflocalvariableis#{i}" end
这将产生以下结果,并会进入一个无限循环:
Valueoflocalvariableis1 Valueoflocalvariableis2 Valueoflocalvariableis1 Valueoflocalvariableis2 Valueoflocalvariableis1 Valueoflocalvariableis2 ............................