6.19. Ruby block

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

Page Views: 10 views

You already know how Ruby defines methods and how you call them. Similarly, Ruby has the concept of a block.

  • Blocks are made up of a lot of code.

  • You need to give the block a name.

  • The code in the block is always contained in curly braces {} inside.

  • A block is always called from a function with the same name as it. This means that if your block name is test then you want to use the function test to call this block.

  • You can use the yield statement to invoke the block.

6.19.1. Grammar #

block_name{statement1statement2.......... } 

Here, you will learn how to use a simple yield statement to invoke the block. You will also learn how to use the yield statement to invoke theblock. In the example, you will see these two types of yield statement.

yield statement #

Let’s see one yield example of the statement:

6.19.2. Example #

#!/usr/bin/ruby#-*- coding: UTF-8 -*-deftestputs"In the test method, "yieldputs" you return to the test method, "yieldendtest {puts" you are in the block "} 

The running result of the above instance is as follows:

Within the test method You're in the block You're back in the test method again You're in the block 

You can also pass a yield statement. Here is an example:

6.19.3. Example #

#!/usr/bin/ruby#-*- coding: UTF-8 -*-deftestyield5puts" In the test method, 'yield100endtest {|i|puts' you are in block #{i}'} 

The running result of the above instance is as follows:

You're within block 5 Within the test method You're within 100 blocks 

Here, yield statement is followed by parameters. You can even pass multiple parameters. In a block, you can place a variable between two vertical bars to accept parameters. So, in the above code, yield 5 statement to test block passes a value of 5 as a parameter.

Now, look at the following statement:

test{\|i\|puts"You are within block # {i}“} 

In this case, the value of 5 will be in the variable i copy that in. Now, look at the following puts statement:

puts"You are within block # {i}“" 

This puts output of the statement is:

You are within block # {i}“ 

If you want to pass multiple parameters, then yield statement is asfollows:

yielda,b 

At this point, the block is as follows:

test{\|a,b\|statement} 

Parameters are separated by commas.

Blocks and methods #

You have seen how blocks and methods relate to each other. You usually use the yield statement invokes a block from a method with the same name. Therefore, the code is as follows:

6.19.4. Example #

#!/usr/bin/rubydeftestyieldendtest{puts"Hello world"} 

This example is the easiest way to implement a block. You use the yield statement call test block.

But if the last parameter of the method is preceded by a & then you canpass a block to the method, and the block can be assigned to the last parameter If * and & are also appears in the parameter list & .It should be in the back.

6.19.5. Example #

#!/usr/bin/rubydeftest(&block)block.callendtest{puts"Hello World!"} 

The running result of the above instance is as follows:

Hello World! 

BEGIN and END blocks #

Each Ruby source file can declare a block of code (BEGIN block) to run when the file is loaded, and a block of code to run after the program finishes execution ( END block).

6.19.6. Example #

#/ Usr/bin/rubyBEGIN {# BEGIN code block puts "BEGIN code block"} END {# END Code block puts "END code block"} # Main code block puts "Main code block" 

A program can contain multiple BEGIN and END block. BEGIN Blocks are executed in the order in which they appear. END blocks are executed in the reverse order in which they appear. When executed, the aboveprogram outputs the following results:

BEGIN block MAIN block END block 
《地理信息系统原理、技术与方法》  97

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