Ruby Blocks
You already know how Ruby defines methods and how you call them. Similarly, Ruby has the concept of blocks.
- A block consists of a body of code.
- You need to give the block a name.
- The code in a block is always enclosed in curly braces {}.
- A block is always invoked from a function with the same name. This means that if your block is named test, you use the function test to invoke the block.
- You can use the yield statement to invoke a block.
Syntax
block_name
{
statement1
statement2
.......... }
Here you'll learn how to use a simple yield statement to invoke a block. You'll also learn how to use a yield statement with parameters to invoke a block. In the examples, you'll see both types of yield if statement.
yield Statement
Let's look at an example of a yield statement:
Example
!/usr/bin/ruby
-- coding: UTF-8 --
def
test
puts
"
Inside the test method
"
yield
puts
"
You're back inside the test method
"
yield
end
test
{
puts
"
You're inside the block
"
}
Try it »
The above example produces the following result:
在 test 方法内
你在块内
你又回到了 test 方法内
你在块内
You can also pass a yield statement with parameters. Here's an example:
Example
!/usr/bin/ruby
-- coding: UTF-8 --
def
test
yield
5
puts
"
Inside the test method
"
yield
100
end
test
{|
i
|
puts
"
You're inside the block #{i}
"
}
Try it »
The above example produces the following result:
你在块 5 内
在 test 方法内
你在块 100 内
Here,yield The statement is followed by arguments. You can even pass multiple arguments. In a block, you can place a variable between two vertical bars to accept arguments. So in the code above, the yield 5 statement passes the value 5 to the test block as an argument.
Now, look at the following statement:
test
{|
i
|
puts
"
You're inside the block #{i}
"
}
Here, the value 5 is received in the variable i. Now, look at the following puts statement:
puts
"
You're inside the block #{i}
"
The output of this puts statement is:
You are inside the
5
block.
If you want to pass multiple arguments, the yield statement looks like this:
yield
a
,
b
At this point, the block looks like this:
test
{|
a
,
b
|
statement
}
Arguments are separated by commas.
Blocks and methods
You've seen how blocks and methods are interrelated. You typically use a yield statement to invoke a block from a method with the same name. Thus, the code looks like this:
Example
!/usr/bin/ruby
def
test
yield
end
test
{
puts
"
Hello world
"
}
This example is the simplest way to implement a block. You use the yield statement to invoke the test block.
But if the last parameter of a method is preceded by &, you can pass a block to the method, and the block is assigned to that last parameter. If both * and & appear in the parameter list, & should come last.
Example
!/usr/bin/ruby
def
test
(
&
block
)
block
.
call
end
test
{
puts
"
Hello World!
"
}
Try it »
The above example produces the following result:
Hello World!
BEGIN and END blocks
Every Ruby source file can declare a block of code to run when the file is loaded (a BEGIN block) and a block of code to run after the program finishes executing (an END block).
Example
!/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 execute in the order they appear. END blocks execute in the reverse order. When executed, the above program produces the following result:
BEGIN 代码块
MAIN 代码块
END 代码块