6.19. Ruby block

发布时间 :2023-10-27 23:00:07 UTC      

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

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.