Ruby blocks


You already know how Ruby defines methods and how you call them. Similarly, Ruby has a concept of blocks.

  • Blocks consist of large amounts of code.

  • You need to give the block a name.

  • Code within a block is always enclosed in curly braces {}.

  • A block is always called from a function with the same name as it. This means that if your block is named test, then you call the block using the function test.

  • 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 blocks using the yield statement with parameters. In the example, you will see both types of yield statements.

yield Statement

Let us look at an example of the yield statement:

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-

def test
   puts "在 test 方法内"
   yield
   puts "你又回到了 test 方法内"
   yield
end
test {puts "你在块内"}


The result of the above example is :

在 test 方法内
你在块内
你又回到了 test 方法内
你在块内

You can also pass a yield statement with parameters. The following is an example:

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-

def test
   yield 5
   puts "在 test 方法内"
   yield 100
end
test {|i| puts "你在块 #{i} 内"}


The result of the above example is:

你在块 5 内
在 test 方法内
你在块 100 内

Here, the yield statement is followed by parameters. You can even pass multiple parameters. In a block, you place a variable between two vertical bars to accept arguments. So, in the above code, the yield 5 statement passes the value 5 as parameter to the test block.

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:

你在块 5 内

If you want to pass multiple parameters, then the yield statement looks like this :

yield a, b

At this point, 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 typically use a yield statement to call a block from a method that has the same name as it. Therefore, the code looks like this:

#!/usr/bin/ruby

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

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

But if the last parameter of a method is preceded by &, then you can pass a block to the method and the block can be assigned to the last parameter. If * and & appear in the parameter list at the same time, & should be placed after.

#!/usr/bin/ruby

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


The result of the above example is:

Hello World!

BEGIN and END blocks

Each Ruby source file can be declared when the file is loaded A block of code to run when the program completes execution (BEGIN block), and a block of code to run after the program completes execution (END block).

#!/usr/bin/ruby

BEGIN { 
  # BEGIN 代码块
  puts "BEGIN 代码块"
} 

END { 
  # END 代码块
  puts "END 代码块"
}
  # MAIN 代码块
puts "MAIN 代码块"

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 in which they appear. When executed, the above program produces the following results:

BEGIN 代码块
MAIN 代码块
END 代码块