6.17. Ruby cycle

发布时间 : 2025-10-25 13:34:00 UTC      

Page Views: 9 views

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

6.17.1. 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 

6.17.2. 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 

6.17.3. 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 

6.17.4. 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 

6.17.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 

6.17.6. 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 

6.17.7. 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 

6.17.8. 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 ............................ 

6.17.9. 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 ............................ 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。