English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
You already know how Ruby defines methods and how you call methods. Similarly, Ruby has the concept of blocks.
block_name{ statement1 statement2 .......... }
Here, you will learn how to use a simple yield statements to call the block. You will also learn how to use 'yield' statements with parameters. yield statements to call the block. In the example, you will see both types of yield statement.
Let's look at an example of a 'yield' statement:
#!/usr/bin/ruby # -*- coding: UTF-8 -*- def test puts "在 test 方法内" yield puts "你又回到了 test 方法内" yield end test {puts "你在块内"}
The above example runs as follows:
inside the test method you are inside the block you are back inside the test method you are inside the block
You can also pass a 'yield' statement with parameters. Here is an example:
#!/usr/bin/ruby # -*- coding: UTF-8 -*- def test yield 5 puts "在 test 方法内" yield 100 end test {|i| puts "你在块 #{i} 内"}
The above example runs as follows:
you are in the block 5 inner inside the test method you are in the block 100 inside
Here,yield statement is followed by parameters. You can even pass multiple parameters. In the block, you can place a variable between two vertical bars to accept parameters. Therefore, in the above code, 'yield' 5 statement passes the value to the test block. 5 as a parameter.
Now, look at the following statement:
test {|i| puts "你在块 #{i} 内"}
Here, the value 5 will be received in the variable i. Now, observe the following puts statement:
puts "你在块 #{i} 内"
The output of this puts statement is:
you are in the block5 inner
If you want to pass multiple parameters, then yield The statement is as follows:
yield a, b
At this time, the block looks like this:
test {|a, b| statement}
Parameters are separated by commas.
You have already seen how blocks and methods are related to each other. You usually use the 'yield' statement to call a block from a method with the same name. Therefore, the code is as follows:
#!/usr/bin/ruby def test yield end test{ puts "Hello world"}
This is the simplest way to implement a block. You use yield statement calls the test block.
But if the last parameter of the method is preceded by '&', then you can pass a block to the method, and this block can be assigned to the last parameter. If * The ampersand '&' appears in the parameter list together with '&', it should be placed at the end.
#!/usr/bin/ruby def test(&block) block.call end test { puts "Hello World!"}
The above example runs as follows:
Hello World!
Each Ruby source file can declare a code block (BEGIN block) to be executed when the file is loaded, as well as a code block (END block) to be executed after the program has finished executing.
#!/usr/bin/ruby BEGIN { # 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 blocks. BEGIN blocks are executed in the order they appear. END blocks are executed in the reverse order. When executed, the above program outputs the following result:
BEGIN Code Block MAIN Code Block END Code Block