Explain the block knowledge in Ruby and the Ruby knowledge

Source: Internet
Author: User

Explain the block knowledge in Ruby and the Ruby knowledge

Syntax:

block_name{  statement1  statement2  ..........}

Here, we will learn how to call a block by using a simple yield statement. You will also learn to use yield statements with 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 of the yield statement:

#!/usr/bin/rubydef test  puts "You are in the method"  yield  puts "You are again back to the method"  yieldendtest {puts "You are in the block"}

This produces the following results:

You are in the methodYou are in the blockYou are again back to the methodYou are in the block

It can also be declared through parameters and submission. The following is an example:

#!/usr/bin/rubydef test  yield 5  puts "You are in the method test"  yield 100endtest {|i| puts "You are in the block #{i}"}

This produces the following results:

You are in the block 5You are in the method testYou are in the block 100

Here the yield statement is written with parameters. You can even pass multiple parameters. Parameters received by the variable (|) placed between two vertical lines in this block. Therefore, in the above Code, the yield5 statement uses the test block as a parameter value of 5.

Now let's look at the following statement:

test {|i| puts "You are in the block #{i}"}

Here, the value in variable I is 5. Follow the following puts statement:

puts "You are in the block #{i}"

The output of the puts statement is:

You are in the block 5

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

yield a, b

The block is:

test {|a, b| statement}

These parameters are separated by commas.
Block and method:

We have seen how to associate a block with a method. Generally, the yield statement is used to call a method with the same name as a block. Therefore, write:

#!/usr/bin/rubydef test yieldendtest{ puts "Hello world"}

This example is the simplest way to implement a block. Call the test block to use the yield statement.
However, if & is added before the method of the last parameter, you can use a block method to assign this block to the last parameter.

* And & In the parameter list & are still behind.

#!/usr/bin/rubydef test(&block)  block.callendtest { puts "Hello World!"}This will produce following result:Hello World!

BEGIN and END blocks

After each Ruby source file can declare a code block as the file is loaded and run (BEGIN block), the program has been executed (END block ).

#!/usr/bin/rubyBEGIN {  # 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 contain multiple in and END blocks. The BEGIN blocks are executed in the order they are encountered. The END block is executed in reverse order. When the above program is executed, the following results are generated:

BEGIN code blockMAIN code blockEND code block

Related Article

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.