Ruby Blocks
You already know how Ruby defines methods and how you call them. Similarly, Ruby has a concept of blocks.
- A block consists of a large amount of code.
- You need to give the block a name.
- The code within the block is always enclosed within braces {}.
- A block is always invoked from a function with the same name as that of the block. This means if your block name is test, then you use the function test to call this block.
- You can use the yield statement to call a block.
Syntax
block_name {
statement1
statement2
..........
}
Here, you will learn how to call a block using a simple yield statement. You will also learn how to call a block with a yield statement that includes parameters. In the examples, you will see both types of yield statements.
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 are back inside the test method"
yield
end
test { puts "You are in the block" }
The output of the above example is:
Inside the test method
You are in the block
You are back inside the test method
You are in the block
You can also pass parameters with the yield statement. Here is 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 are in the block #{i}" }
The output of the above example is:
You are in the block 5
Inside the test method
You are in the block 100
Here, the yield statement is followed by parameters. You can even pass multiple parameters. Inside the block, you can place a variable between two vertical bars to receive the parameters. Thus, in the above 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}" }
Here, the value 5 is received in the variable i. Now, observe the following puts statement:
puts "You are in the block #{i}"
The output of this puts statement is:
You are in the block 5
If you want to pass multiple parameters, the yield statement looks like this:
yield a, b
In this case, the block looks like this:
test { |a, b| statement }
Parameters are separated by commas.
Blocks and Methods
You have 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. 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 call the test block.
However, if the last parameter in a method is prefixed with an &, then you can pass a block to this method, and this block will be assigned to the last parameter. If both * and & are present in the parameter list, & should come later.
Example
#!/usr/bin/ruby
def test(&block)
block.call
end
test { puts "Hello World!" }
The output of the above example is:
Hello World!
BEGIN and END Blocks
Each Ruby source file can declare blocks of code that are run when the file is loaded (BEGIN blocks) and after the program has finished executing (END blocks).
Example
#!/usr/bin/ruby
BEGIN {
# BEGIN block code
puts "BEGIN block code"
}
END {
# END block code
puts "END block code"
}
# MAIN block code
puts "MAIN block code"
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 block code
MAIN block code
END block code