Ruby中的循环用于执行相同的代码块指定的次数。本章详细介绍了Ruby支持的所有循环语句。
while conditional [do] code end
当有条件的为true时执行 code 。 while 循环的有条件与 code 由保留字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
如果 while 修饰符跟随 begin 语句而没有 rescue 或sure子句,则在条件条件被执行之前先执行 code 判断。
#!/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 ,换行符或分号与 code 隔开。
#!/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
如果 until 修饰符跟随 begin 语句而没有 rescue 或sure子句,则 code 在有条件的进行判断。之前执行一次>
#!/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
对表达式中的每个元素执行一次 code 。
链接:https://www.learnfk.comhttps://www.learnfk.com/ruby/ruby-loops.html
来源:LearnFk无涯教程网
#!/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,换行符或分号与 code 隔开。
#!/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 或 call 。
#!/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表达式的救援子句中,请从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 ............................
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)