Ruby cycle


Release date:2023-10-27 Update date:2023-10-27 Editor:admin View counts:304

Label:

Ruby cycle

Loops in Ruby are used to execute the same block of code several times. Thissection details all the loop statements supported by Ruby.

Ruby while statement

Grammar

whileconditional[do]codeend

Or

Grammar

whileconditional[:]codeend

When conditional is true, execute code .

In grammar do or : can be omitted without writing. But if you want to write it in one line `` While`` type, you must use the do or : separate conditional or program blocks.

Example

#!/usr/bin/ruby#-*- coding: UTF-8
-*-$i=0$num=5while$i<$numdoputs("In a loop statement i = #$i")$i+=1end

The output result of the above example is:

In a loop statement, i=0
In a loop statement, i=1
In a loop statement, i=2
In a loop statement, i=3
In a loop statement, i=4

Ruby while modifier

Grammar

codewhilecondition or begincodeendwhileconditional

When conditional is true, execute code .

If while modifier follows a no rescue or ensure of the clause begin after the statement code will be in conditional execute once before judgment.

Example

#!/usr/bin/ruby#-*- coding: UTF-8 -*-$i=0$num=5beginputs("In a loop statement i
= #$i")$i+=1endwhile$i<$num

The output result of the above example is:

In a loop statement, i=0
In a loop statement, i=1
In a loop statement, i=2
In a loop statement, i=3
In a loop statement, i=4

Ruby until statement

Grammar

untilconditional[do]codeend

When conditional is false, execute code .

In grammar do can omit it. But if you want to write a until type, you must use the do separate conditional or program blocks.

Example

#!/usr/bin/ruby#-*- coding: UTF-8
-*-$i=0$num=5until$i>$numdoputs("In a loop statement i = #$i")$i+=1;end

The output result of the above example is:

In a loop statement, i=0
In a loop statement, i=1
In a loop statement, i=2
In a loop statement, i=3
In a loop statement, i=4
In a loop statement, i=5

Ruby until modifier

Grammar

codeuntilconditional or begincodeenduntilconditional

When conditional is false, execute code .

If until modifier follows a no rescue or ensure of the clause begin after the statement code will be in conditional execute once before judgment.

Example

#!/usr/bin/ruby#-*- coding: UTF-8 -*-$i=0$num=5beginputs("In a loop statement i
= #$i")$i+=1;enduntil$i>$num

The output result of the above example is:

In a loop statement, i=0
In a loop statement, i=1
In a loop statement, i=2
In a loop statement, i=3
In a loop statement, i=4
In a loop statement, i=5

Ruby for statement

Grammar

forvariable[,variable...]inexpression[do]codeend

First evaluate the expression to get an object, and then target the expression execute each element in the code .

Example

#!/usr/bin/ruby#-*- coding: UTF-8 -*-foriin0..5puts"The value of a 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 from 0 to 5 (inclusive).

The output result of the above example is:

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 loop is almost exactly equivalent to:

(expression).eachdo\|variable[,variable...]\|codeend

However, the for loop does not create a new scope for local variables.

In grammar do can omit it. But if you want to write a for type, you must use the do separate conditional or program blocks.

Example

#!/usr/bin/ruby#-*- coding: UTF-8
-*-(0..5).eachdo\|i\|puts"The value of a local variable is #{i}"end

The output result of the above example is:

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

Ruby break statement

Grammar

break

End the innermost loop. If called within a block, the method of the related block is terminated (the method returns nil ).

Example

#!/usr/bin/ruby#-*- coding: UTF-8
-*-foriin0..5ifi>2thenbreakendputs"The value of a local variable is #{i}"end

The output result of the above example is:

The value of the local variable is 0
The value of the local variable is 1
The value of the local variable is 2

Ruby next statement

Grammar

next

Skip to the next iteration of the loop. If called within a block, the execution of the block is terminated yield expression returns nil ).

Example

#!/usr/bin/ruby#-*- coding: UTF-8
-*-foriin0..5ifi<2thennextendputs"The value of a local variable is #{i}"end

The output result of the above example is:

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

Ruby redo statement

Grammar

redo

Restart the iteration of the innermost loop without checking the loop conditions. If called within a block, start over yield or call .

Example

#!/usr/bin/ruby#-*- coding: UTF-8
-*-foriin0..5ifi<2thenputs"The value of a local variable is #{i}"redoendend

This will produce the following results and enter an infinite loop:

The value of the local variable is 0
The value of the local variable is 0
............................

Ruby retry statement

Note:1.9 and later versions do not support using retry in loops.

Grammar

retry

If retry appear in begin of the expression rescue clause, then from the begin the beginning of the subject starts over.

begindo_something#Exception thrownrescue
#process errorretry#Starting from begin againend

If retry appears within an iteration, within a block, or for within the body of the expression, the iterative call is restarted. The parameters of the iteration are reevaluated.

foriin1..5retryifsome_condition#Starting from i==1 again end

Example

#!/usr/bin/ruby#-*- coding: UTF-8
-*-foriin1..5retryifi>2puts"The value of a local variable is #{i}"end

This will produce the following results and 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
............................

Powered by TorCMS (https://github.com/bukun/TorCMS).