Ruby - 循环语句

Ruby - 循环语句 首页 / Ruby入门教程 / Ruby - 循环语句

Ruby中的循环用于执行相同的代码块指定的次数。本章详细介绍了Ruby支持的所有循环语句。

Ruby while 语句

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

这将产生以下输出-

链接:https://www.learnfk.comhttps://www.learnfk.com/ruby/ruby-loops.html

来源:LearnFk无涯教程网

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

这将产生以下输出-

链接:https://www.learnfk.comhttps://www.learnfk.com/ruby/ruby-loops.html

来源:LearnFk无涯教程网

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

Ruby until 语句

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

这将产生以下输出-

链接:https://www.learnfk.comhttps://www.learnfk.com/ruby/ruby-loops.html

来源:LearnFk无涯教程网

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

这将产生以下输出-

链接:https://www.learnfk.comhttps://www.learnfk.com/ruby/ruby-loops.html

来源:LearnFk无涯教程网

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

Ruby for 语句

for variable [, variable ...] in expression [do]
   code
end

对表达式中的每个元素执行一次 code 。

#!/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

这将产生以下输出-

链接:https://www.learnfk.comhttps://www.learnfk.com/ruby/ruby-loops.html

来源:LearnFk无涯教程网

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

Ruby Break 语句

break

终止最内部的循环。如果在块中调用了相关的块,则终止该方法(该方法返回nil)。

#!/usr/bin/ruby

for i in 0..5
   if i > 2 then
      break
   end
   puts "Value of local variable is #{i}"
end

这将产生以下输出-

链接:https://www.learnfk.comhttps://www.learnfk.com/ruby/ruby-loops.html

来源:LearnFk无涯教程网

Value of local variable is 0
Value of local variable is 1
Value of local variable is 2

Ruby next 语句

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

这将产生以下输出-

链接:https://www.learnfk.comhttps://www.learnfk.com/ruby/ruby-loops.html

来源:LearnFk无涯教程网

Value of local variable is 2
Value of local variable is 3
Value of local variable is 4
Value of local variable is 5

Ruby redo 语句

redo

如果在一个块内被调用,则重新开始 yieldcall

#!/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
............................

Ruby retry 语句

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
............................

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

左耳听风 -〔陈皓〕

赵成的运维体系管理课 -〔赵成〕

从0开发一款iOS App -〔朱德权〕

安全攻防技能30讲 -〔何为舟〕

Java业务开发常见错误100例 -〔朱晔〕

Django快速开发实战 -〔吕召刚〕

程序员的个人财富课 -〔王喆〕

超级访谈:对话汤峥嵘 -〔汤峥嵘〕

深入浅出可观测性 -〔翁一磊〕

好记忆不如烂笔头。留下您的足迹吧 :)