Ruby 简明教程

Ruby - Blocks

您已经了解 Ruby 如何定义方法,在该方法中您可以放置多条语句,然后调用该方法。与此类似,Ruby 有一个块的概念。

You have seen how Ruby defines methods where you can put number of statements and then you call that method. Similarly, Ruby has a concept of Block.

  1. A block consists of chunks of code.

  2. You assign a name to a block.

  3. The code in the block is always enclosed within braces ({}).

  4. A block is always invoked from a function with the same name as that of the block. This means that if you have a block with the name test, then you use the function test to invoke this block.

  5. You invoke a block by using the yield statement.

Syntax

block_name {
   statement1
   statement2
   ..........
}

在此,您将学习使用简单的 yield 语句调用块。您还将学习如何使用带有参数的 yield 语句调用块。您将使用两种类型的 yield 语句检查示例代码。

Here, you will learn to invoke a block by using a simple yield statement. You will also learn to use a yield statement with parameters for invoking a block. You will check the sample code with both types of yield statements.

The yield Statement

我们来看一个 yield 语句的示例 −

Let’s look at an example of the yield statement −

#!/usr/bin/ruby

def test
   puts "You are in the method"
   yield
   puts "You are again back to the method"
   yield
end
test {puts "You are in the block"}

这会产生以下结果 −

This will produce the following result −

You are in the method
You are in the block
You are again back to the method
You are in the block

您还可以使用 yield 语句传递参数。这里有一个示例 −

You also can pass parameters with the yield statement. Here is an example −

#!/usr/bin/ruby

def test
   yield 5
   puts "You are in the method test"
   yield 100
end
test {|i| puts "You are in the block #{i}"}

这会产生以下结果 −

This will produce the following result −

You are in the block 5
You are in the method test
You are in the block 100

在此,yield 语句后面写入了参数。您甚至可以传递多个参数。在块中,您将变量放在两个竖线(||)之间以接受参数。因此,在前面的代码中,yield 5 语句将值 5 作为参数传递给 test 块。

Here, the yield statement is written followed by parameters. You can even pass more than one parameter. In the block, you place a variable between two vertical lines (||) to accept the parameters. Therefore, in the preceding code, the yield 5 statement passes the value 5 as a parameter to the test block.

现在,看以下语句 −

Now, look at the following statement −

test {|i| puts "You are in the block #{i}"}

在此,值 5 在变量 i 中接收。现在,观察以下 puts 语句 −

Here, the value 5 is received in the variable i. Now, observe the following puts statement −

puts "You are in the block #{i}"

此 puts 语句的输出为 −

The output of this puts statement is −

You are in the block 5

如果您想要传递多个参数,则 yield 语句变为 −

If you want to pass more than one parameters, then the yield statement becomes −

yield a, b

并且该块为 −

and the block is −

test {|a, b| statement}

参数将用逗号分隔。

The parameters will be separated by commas.

Blocks and Methods

您已经了解块和方法如何相互关联。您通常使用 yield 语句从与块名称相同的调用块。 因此,您编写 −

You have seen how a block and a method can be associated with each other. You normally invoke a block by using the yield statement from a method that has the same name as that of the block. Therefore, you write −

#!/usr/bin/ruby

def test
   yield
end
test{ puts "Hello world"}

这个示例是实现块的最简单方法。您使用 yield 语句来调用测试块。

This example is the simplest way to implement a block. You call the test block by using the yield statement.

但如果一个方法的最后一个参数之前带有 &,则可以将一个块传递给此方法,并且该块将被分配给最后一个参数。如果 * 和 & 都存在于参数列表中,则 & 应出现在后面。

But if the last argument of a method is preceded by &, then you can pass a block to this method and this block will be assigned to the last parameter. In case both * and & are present in the argument list, & should come later.

#!/usr/bin/ruby

def test(&block)
   block.call
end
test { puts "Hello World!"}

这会产生以下结果 −

This will produce the following result −

Hello World!

BEGIN and END Blocks

每个 Ruby 源文件都可以声明代码块以在文件加载时运行(BEGIN 块)并在程序执行结束后运行(END 块)。

Every Ruby source file can declare blocks of code to be run as the file is being loaded (the BEGIN blocks) and after the program has finished executing (the END blocks).

#!/usr/bin/ruby

BEGIN {
   # BEGIN block code
   puts "BEGIN code block"
}

END {
   # END block code
   puts "END code block"
}
   # MAIN block code
puts "MAIN code block"

一个程序可能包含多个 BEGIN 和 END 块。BEGIN 块按照遇到的顺序执行。END 块按照反向顺序执行。执行时,上述示例产生以下结果:

A program may include multiple BEGIN and END blocks. BEGIN blocks are executed in the order they are encountered. END blocks are executed in reverse order. When executed, the above program produces the following result −

BEGIN code block
MAIN code block
END code block