Knowledge of the blocks in Ruby

Source: Internet
Author: User

This article mainly introduces the knowledge of the blocks in Ruby, including yield statements and begin/end blocks, and so on, the friends you need can refer to the



1 2 3 4 5 block_name{statement1 statement2 .....}

Here, you will learn how to call blocks by using a simple yield statement. You will also learn to use the yield statement to have parameter call blocks. The sample code that will be checked, the two types of yield statements.

Yield statement:

Let's take a look at an example in the yield statement:


1 2 3 4 5 6 7 8 9 #!/usr/bin/ruby def Test puts "you are in the method" yield puts "you are again then to the method" yield end test {puts "are in"}

This will produce the following results:


1 2 3 4 You are are in the "method" you are in the blocks you are again the

You can also pass the argument with the yield declaration. Here is an example:


1 2 3 4 5 6 7 8 #!/usr/bin/ruby def Test yield 5 puts "you are in ' method test" yield-end test {|i| puts ' you are in the ' block #{ i} "}

This will produce the following results:


1 2 3 You are are in the "Block 5" are in the "method test" you are in the Block 100

Here the yield statement is written to follow the arguments. You can even pass multiple parameters. A variable that is placed between two vertical lines in the block (| |) The parameters received. Therefore, in the above code, the YIELD5 statement takes the test block as a parameter value of 5.

Now look at the following statement:


1 Test {|i| puts ' you are in the ' Block #{i} '}

Here, the value in the variable i is 5. Now observe the following puts statement:


1 Puts "You are in the Block #{i}"

The output of the puts statement is:


1 are in the Block 5

If you want to exceed one argument, then the yield statement becomes:


1 Yield A, b

So the block is:


1 Test {|a, b| statement}

These parameters are separated by commas.

Blocks and methods:

We've seen how to associate a block with a method. Typically, the call block has a method with the same name from the block by using the yield statement. Therefore, write:


1 2 3 4 5 6 #!/usr/bin/ruby def test yield end test{puts "Hello World"}

This example is the simplest way to implement a block. Call block test uses the yield statement.

However, if the last parameter's method is preceded by an ampersand, the block will be assigned to the last parameter by this method of a block.

* and & in the parameter list & is still in the back.


1 2 3 4 5 6 7 8 9 10 #!/usr/bin/ruby def test (&block) end test {puts "Hello world!"} This'll produce following Result:hello world!

BEGIN and End Blocks

After each ruby source file can be declared as a file to be loaded to run (begin block), the program has finished (end block).


1 2 3 4 5 6 7 8 9 10 11 12-13 #!/usr/bin/ruby begin {# Begin block code puts ' begin code block '} end {# End block code puts ' end ' code block} # Main block code puts ' main code block '

A program may include multiple begin and end blocks. The begin blocks are executed in the order in which they are encountered. End blocks are executed in reverse order. When the above program executes, the following results are produced:


Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.