📅  最后修改于: 2020-10-16 05:49:50             🧑  作者: Mango
Ruby中的循环用于执行相同的代码块指定的次数。本章详细介绍了Ruby支持的所有循环语句。
while conditional [do]
code
end
条件为真时执行代码。 while循环的条件与代码由保留字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
code while condition
OR
begin
code
end while conditional
条件为真时执行代码。
如果while修饰符跟随没有救援或sure子句的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
条件为false时执行代码。直到语句的条件与代码由保留字do ,换行符或分号分隔。
#!/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
条件为false时执行代码。
如果直到修改遵循开始声明,没有抢救或保证条款,代码一旦条件进行评估之前执行。
#!/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
对expression中的每个元素执行一次代码。
#!/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,换行符或分号隔开。
#!/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
跳转到最内部循环的下一个迭代。一个块的终止执行,如果称为块(与产量或呼叫返回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或在块内调用时调用。
#!/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
如果重试出现在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
............................