Latest web development tutorials

Ruby cycle

Ruby in the loop for performing the same block of code several times. This section will detail all the loops Ruby support.

Rubywhile statement

grammar

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.

Examples

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

Rubywhile modifier

grammar

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.

Examples

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

Rubyuntil statements

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.

Examples

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

Rubyuntil modifiers

grammar

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.

Examples

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

Rubyfor statement

grammar

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

First calculation expression to obtain an object, then theexpressionfor each element separately acode.

Examples

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-

for i in 0..5
   puts "局部变量的值为 #{i}"
end

Here, we have defined the range 0..5. Statement for i in 0..5 allow the value of i from 0 to 5 (with 5).

The above example output is:

局部变量的值为 0
局部变量的值为 1
局部变量的值为 2
局部变量的值为 3
局部变量的值为 4
局部变量的值为 5

for ... inloop is almost exactly equivalent to:

(expression).each do |variable[, variable...]| 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.

Examples

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-

(0..5).each do |i|
   puts "局部变量的值为 #{i}"
end

The above example output is:

局部变量的值为 0
局部变量的值为 1
局部变量的值为 2
局部变量的值为 3
局部变量的值为 4
局部变量的值为 5

Rubybreak statement

grammar

break

Termination of the innermost loop. If you have invoked within the block, the block is related to the termination (the method returns nil).

Examples

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-

for i in 0..5
   if i > 2 then
      break
   end
   puts "局部变量的值为 #{i}"
end

The above example output is:

局部变量的值为 0
局部变量的值为 1
局部变量的值为 2

Rubynext statement

grammar

next

Jump to the next iteration of the loop. If you call in the block terminates the execution block(yieldexpression returns nil).

Examples

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-

for i in 0..5
   if i < 2 then
      next
   end
   puts "局部变量的值为 #{i}"
end

The above example output is:

局部变量的值为 2
局部变量的值为 3
局部变量的值为 4
局部变量的值为 5

Rubyredo statement

grammar

redo

Restarting the innermost loop iteration, the loop condition is not checked. If you call in the block, then restartyieldorcall.

Examples

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-

for i in 0..5
   if i < 2 then
      puts "局部变量的值为 #{i}"
      redo
   end
end

This produces the following results, and will enter an infinite loop:

局部变量的值为 0
局部变量的值为 0
............................

Rubyretry statement

NOTE: 1.9 and later versions do not support the use retry in a loop.

grammar

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..5
   retry if some_condition # 重新从 i == 1 开始
end

Examples

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-

for i in 1..5
   retry if  i > 2
   puts "局部变量的值为 #{i}"
end

This produces the following results, and will enter an infinite loop:

局部变量的值为 1
局部变量的值为 2
局部变量的值为 1
局部变量的值为 2
局部变量的值为 1
局部变量的值为 2
............................