HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Ruby in the loop for performing the same block of code several times. This section will detail all the loops Ruby support.
while conditional [do] code end
or
<pre> while conditional [:] code end
When theconditionalis true, it executescode.
Or do syntax: you can be omitted. But to write a formula while in the line, or you must do: apart conditional expression or program blocks.
#!/usr/bin/ruby # -*- coding: UTF-8 -*- $i = 0 $num = 5 while $i < $num do puts("在循环语句中 i = #$i" ) $i +=1 end
The above example output is:
在循环语句中 i = 0 在循环语句中 i = 1 在循环语句中 i = 2 在循环语句中 i = 3 在循环语句中 i = 4
code while condition 或者 begin code end while conditional
When theconditionalis true, it executescode.
Ifwhilein a modifier with norescueor laterbeginstatement ensureclause,code will be executed once before theconditionaljudgment.
#!/usr/bin/ruby # -*- coding: UTF-8 -*- $i = 0 $num = 5 begin puts("在循环语句中 i = #$i" ) $i +=1 end while $i < $num
The above example output is:
在循环语句中 i = 0 在循环语句中 i = 1 在循环语句中 i = 2 在循环语句中 i = 3 在循环语句中 i = 4
until conditional [do] code end
When theconditionalis false, executecode.
Syntax do can be omitted. But to write until the formula, you must do the conditional expression or program blocks apart in the row.
#!/usr/bin/ruby # -*- coding: UTF-8 -*- $i = 0 $num = 5 until $i > $num do puts("在循环语句中 i = #$i" ) $i +=1; end
The above example output is:
在循环语句中 i = 0 在循环语句中 i = 1 在循环语句中 i = 2 在循环语句中 i = 3 在循环语句中 i = 4 在循环语句中 i = 5
code until conditional 或者 begin code end until conditional
When theconditionalis false, executecode.
Ifuntilnow modifier in a statement that norescueor laterbeginensureclause,code will be executed once before theconditionaljudgment.
#!/usr/bin/ruby # -*- coding: UTF-8 -*- $i = 0 $num = 5 begin puts("在循环语句中 i = #$i" ) $i +=1; end until $i > $num
The above example output is:
在循环语句中 i = 0 在循环语句中 i = 1 在循环语句中 i = 2 在循环语句中 i = 3 在循环语句中 i = 4 在循环语句中 i = 5
for variable [, variable /en.] in expression [do] code end
First calculation expression to obtain an object, then theexpressionfor each element separately acode.
#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 0/en5 puts "局部变量的值为 #{i}" end
Here, we have defined the range 0/en5. Statement for i in 0/en5 allow the value of i from 0 to 5 (with 5).
The above example output is:
局部变量的值为 0 局部变量的值为 1 局部变量的值为 2 局部变量的值为 3 局部变量的值为 4 局部变量的值为 5
for /en. inloop is almost exactly equivalent to:
(expression).each do |variable[, variable/en.]| code end
However, for loop does not create a new scope for local variables.
Syntax do can be omitted. But to write for the formula in a row, then you must do apart conditional expression or program blocks.
#!/usr/bin/ruby # -*- coding: UTF-8 -*- (0/en5).each do |i| puts "局部变量的值为 #{i}" end
The above example output is:
局部变量的值为 0 局部变量的值为 1 局部变量的值为 2 局部变量的值为 3 局部变量的值为 4 局部变量的值为 5
break
Termination of the innermost loop. If you have invoked within the block, the block is related to the termination (the method returns nil).
#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 0/en5 if i > 2 then break end puts "局部变量的值为 #{i}" end
The above example output is:
局部变量的值为 0 局部变量的值为 1 局部变量的值为 2
next
Jump to the next iteration of the loop. If you call in the block terminates the execution block(yieldexpression returns nil).
#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 0/en5 if i < 2 then next end puts "局部变量的值为 #{i}" end
The above example output is:
局部变量的值为 2 局部变量的值为 3 局部变量的值为 4 局部变量的值为 5
redo
Restarting the innermost loop iteration, the loop condition is not checked. If you call in the block, then restartyieldorcall.
#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 0/en5 if i < 2 then puts "局部变量的值为 #{i}" redo end end
This produces the following results, and will enter an infinite loop:
局部变量的值为 0 局部变量的值为 0 /en/en/en/en/en/en/en/en/en/en/en/en/en/en
NOTE: 1.9 and later versions do not support the use retry in a loop.
retry
Ifretryappears in the expression begin rescue clause, from the beginning of the body begin to re-start.
begin do_something # 抛出的异常 rescue # 处理错误 retry # 重新从 begin 开始 end
If retry appears in the iteration, or for expression within the body, re-iterates calls within the block. Iteration parameters reassessed.
for i in 1/en5 retry if some_condition # 重新从 i == 1 开始 end
#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 1/en5 retry if i > 2 puts "局部变量的值为 #{i}" end
This produces the following results, and will enter an infinite loop:
局部变量的值为 1 局部变量的值为 2 局部变量的值为 1 局部变量的值为 2 局部变量的值为 1 局部变量的值为 2 /en/en/en/en/en/en/en/en/en/en/en/en/en/en