English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Ruby Blocks

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

  • 块由大量的代码组成。
  • Blocks are made up of a lot of code.
  • You need to name the block.
  • the code in the block is always enclosed in curly braces {}. if you want to use a functionBlocks always call the function that has the same name. This means that if your block name is if you want to use a function test
  • to call this block. yield statements to call the block. You can use

syntax

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.

yield statement

Let's look at an example of a 'yield' statement:

Online Examples

#!/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:

Online Examples

#!/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.

Block and method

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:

Online Examples

#!/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.

Online Examples

#!/usr/bin/ruby
def test(&block)
   block.call
end
test { puts "Hello World!"}

The above example runs as follows:

Hello World!

BEGIN and END Blocks

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.

Online Examples

#!/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