English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Loops in Ruby are used to execute the same code block multiple times. This chapter will introduce all the loop statements supported by Ruby in detail.
while conditional [do] code end
code until conditional
while conditional [:] code end
end until conditional conditional execute when true code.
In the syntax, 'do' or ':' can be omitted. However, if you want to write a 'while' statement on one line, it must be separated by 'do' or ':' from the condition or code block.
#!/usr/bin/ruby # -*- coding: UTF-8 -*- $i = 0 $num = 5 while $i < $num do puts("In the loop statement, i = #$i") $i +=1 end
The output of the above example is as follows:
in the loop statement, i = 0 in the loop statement, i = 1 in the loop statement, i = 2 in the loop statement, i = 3 in the loop statement, i = 4
code while condition code until conditional begin code end while conditional
end until conditional conditional execute when true code.
if while until rescue modifier follows a modifier without begin or the ensure clause'scode follows the conditional judges once before execution.
#!/usr/bin/ruby # -*- coding: UTF-8 -*- $i = 0 $num = 5 begin puts("In the loop statement, i = #$i") $i +=1 end while $i < $num
The output of the above example is as follows:
in the loop statement, i = 0 in the loop statement, i = 1 in the loop statement, i = 2 in the loop statement, i = 3 in the loop statement, i = 4
until conditional [do] code end
end until conditional conditional 为假时,执行 code.
when it is false, execute
#!/usr/bin/ruby # -*- coding: UTF-8 -*- $i = 0 $num = 5 In the syntax, do can be omitted. However, if you want to write the until form on one line, you must separate the conditional expression or code block with do. puts("In the loop statement, i = #$i") $i +=1; end
The output of the above example is as follows:
in the loop statement, i = 0 in the loop statement, i = 1 in the loop statement, i = 2 in the loop statement, i = 3 in the loop statement, i = 4 in the loop statement, i = 5
modifier code until conditional begin code or
end until conditional conditional when code.
if when it is false, execute until rescue modifier follows a modifier without begin or the ensure clause'scode follows the conditional judges once before execution.
#!/usr/bin/ruby # -*- coding: UTF-8 -*- $i = 0 $num = 5 begin puts("In the loop statement, i = #$i") $i +=1; end until $i > $num
The output of the above example is as follows:
in the loop statement, i = 0 in the loop statement, i = 1 in the loop statement, i = 2 in the loop statement, i = 3 in the loop statement, i = 4 in the loop statement, i = 5
for variable [, variable ...] in expression [do] code end
first calculates the expression to get an object, and then processes expression each element of which is executed once code.
#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 0..5 puts "The value of the local variable is #{i}" end
Here, we have defined the range 0..5. The statement for i in 0..5 allows the value of i to range from 0 to 5(Including 5).
The output of the above example is as follows:
the value of the local variable is 0 The value of the local variable is 1 The value of the local variable is 2 The value of the local variable is 3 The value of the local variable is 4 The value of the local variable is 5
for...in The loop is almost completely equivalent to:
(expression).each do |variable[, variable...]| code end
However, the for loop does not create a new scope for local variables.
In the syntax, do can be omitted. However, if you want to write the for form on one line, you must separate the conditional expression or code block with do.
#!/usr/bin/ruby # -*- coding: UTF-8 -*- (0..5).each do |i| puts "The value of the local variable is #{i}" end
The output of the above example is as follows:
the value of the local variable is 0 The value of the local variable is 1 The value of the local variable is 2 The value of the local variable is 3 The value of the local variable is 4 The value of the local variable is 5
break
Terminates the innermost loop. If called within a block, it terminates the method related to the block (the method returns nil).
#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 0..5 if i > 2 then break end puts "The value of the local variable is #{i}" end
The output of the above example is as follows:
the value of the local variable is 0 The value of the local variable is 1 The value of the local variable is 2
next
Jumps to the next iteration of the loop. If called within a block, it terminates the execution of the block (yield expression returns nil).
#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 0..5 if i < 2 then next end puts "The value of the local variable is #{i}" end
The output of the above example is as follows:
The value of the local variable is 2 The value of the local variable is 3 The value of the local variable is 4 The value of the local variable is 5
redo
restarts the current iteration of the innermost loop without checking the loop condition. If called within a block, it restarts. yield or call.
#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 0..5 if i < 2 then puts "The value of the local variable is #{i}" redo end end
This will produce the following result and will enter an infinite loop:
the value of the local variable is 0 the value of the local variable is 0 ............................
Note:1.9and later versions do not support using retry within loops.
retry
if retry If it appears in the rescue clause of the begin expression, it will restart from the beginning of the begin body.
begin do_something # Raised exception rescue # Error Handling retry # Resume from begin end
If retry appears within an iteration, block, or the body of a for expression, the iteration will restart. The iteration parameters will be re-evaluated.
for i in 1..5 retry if some_condition # Resume from i == 1 Start end
#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 1..5 retry if i > 2 puts "The value of the local variable is #{i}" end
This will produce the following result and will enter an infinite loop:
The value of the local variable is 1 The value of the local variable is 2 The value of the local variable is 1 The value of the local variable is 2 The value of the local variable is 1 The value of the local variable is 2 ............................