May 12, 2021 Ruby
Loops in Ruby are used to execute the same block of code several times. This section details all the circular statements supported by Ruby.
while conditional [do] code end
Or
<pre> while conditional [:] code end
When the conditional is true, code is executed.
Do or : in the syntax can be omitted from writing.
However, to write the while equation within a row, you must separate conditional or program chunks with do or : .
#!/usr/bin/ruby # -*- coding: UTF-8 -*- $i = 0 $num = 5 while $i < $num do puts("在循环语句中 i = #$i" ) $i +=1 end
The output of the above examples is:
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 或者 begin code end while conditional
When the conditional is true, code is executed.
If the while modifier follows a begin statement that does not have a rescue or security clause, code executes once before the conditional judgment.
#!/usr/bin/ruby # -*- coding: UTF-8 -*- $i = 0 $num = 5 begin puts("在循环语句中 i = #$i" ) $i +=1 end while $i < $num
The output of the above examples is:
在循环语句中 i = 0 在循环语句中 i = 1 在循环语句中 i = 2 在循环语句中 i = 3 在循环语句中 i = 4
until conditional [do] code end
When the conditional is false, code is executed.
Do in syntax can be omitted from writing. However, to write out the until equation in a row, you must separate the conditional or program chunks with do.
#!/usr/bin/ruby # -*- coding: UTF-8 -*- $i = 0 $num = 5 until $i > $num do puts("在循环语句中 i = #$i" ) $i +=1; end
This results in the following:
在循环语句中 i = 0 在循环语句中 i = 1 在循环语句中 i = 2 在循环语句中 i = 3 在循环语句中 i = 4 在循环语句中 i = 5
code until conditional 或者 begin code end until conditional
When the conditional is false, code is executed.
If the until modifier follows a begin statement that does not have a rescue or security clause, code executes once before the conditional judgment.
#!/usr/bin/ruby # -*- coding: UTF-8 -*- $i = 0 $num = 5 begin puts("在循环语句中 i = #$i" ) $i +=1; end until $i > $num
This results in the following:
在循环语句中 i = 0 在循环语句中 i = 1 在循环语句中 i = 2 在循环语句中 i = 3 在循环语句中 i = 4 在循环语句中 i = 5
for variable [, variable ...] in expression [do] code end
Code is executed once for each element in the expression.
#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 0..5 puts "局部变量的值为 #{i}" end
Here, we have defined the range 0..5.
The statement for i in 0..5 allows i to have values from 0 to 5 (including 5).
Try it out . . .
The output of the above examples is:
局部变量的值为 0 局部变量的值为 1 局部变量的值为 2 局部变量的值为 3 局部变量的值为 4 局部变量的值为 5
for... The in loop is almost entirely equivalent to:
(expression).each do |variable[, variable...]| code end
However, the for loop does not create a new scope for local variables.
Do in syntax can be omitted from writing. However, to write out the for equation in a row, you must separate the conditional or program chunks with do.
#!/usr/bin/ruby # -*- coding: UTF-8 -*- (0..5).each do |i| puts "局部变量的值为 #{i}" end
The output of the above examples is:
局部变量的值为 0 局部变量的值为 1 局部变量的值为 2 局部变量的值为 3 局部变量的值为 4 局部变量的值为 5
break
Terminates the innerst loop. If called within a block, the method of the related block is terminated (the method returns nil).
#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 0..5 if i > 2 then break end puts "局部变量的值为 #{i}" end
The output of the above examples is:
局部变量的值为 0 局部变量的值为 1 局部变量的值为 2
next
Jump to the next iteration of the inner innerst loop. If called within the block, the execution of the block (yield or call returns nil) is terminated.
#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 0..5 if i < 2 then next end puts "局部变量的值为 #{i}" end
The output of the above examples is:
局部变量的值为 2 局部变量的值为 3 局部变量的值为 4 局部变量的值为 5
redo
Restart the iteration of the inner innerst loop without checking the loop conditions. If called within the block, start yield or call again.
#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 0..5 if i < 2 then puts "局部变量的值为 #{i}" redo end end
This results in the following results and enters an infinite loop:
局部变量的值为 0 局部变量的值为 0 ............................
retry
If retry appears in the rescue clause of the begin expression, it starts over at the beginning of the begin body.
begin do_something # 抛出的异常 rescue # 处理错误 retry # 重新从 begin 开始 end
If the retry appears in the iteration, within the block, or in the body of the for expression, restart the iteration call. The parameters of the iteration are re-evaluated.
for i in 1..5 retry if some_condition # 重新从 i == 1 开始 end
#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 1..5 retry if i > 2 puts "局部变量的值为 #{i}" end
This results in the following results and enters an infinite loop:
局部变量的值为 1 局部变量的值为 2 局部变量的值为 1 局部变量的值为 2 局部变量的值为 1 局部变量的值为 2 ............................