Block code blocks in Ruby Learning tutorials _ruby Topics

Source: Internet
Author: User

1, what is the code block
in Ruby, the code between {} or Do...end is a block of code. A code block can only appear after a method, and it is invoked immediately on the same line as the last parameter of the method, which is called by the yield keyword. For example:

[1,2,3,4,5].each {|i| puts i}

[1,2,3,4,5].each do |i| 
Puts I end


Block variables: Calling blocks with the yield keyword can also pass parameters, the vertical bar (|) The parameter name given is used to receive parameters from the yield.

Between vertical bars (as in the previous example | i |) Variable is called a block variable, and the function is the same as the parameter of a normal method

2, master the code block writing
the most common, simplest, most controversial and most ruby-style approach is blocks. The wording is as follows:

Array = [1, 2, 3, 4]

array.collect! do |n|
 n * * * 2 end

puts Array.inspect

# => [1, 4, 9, 16]

Do...end constitute a block. The block is then passed through the collect! to an array. You can use N in block to iterate through each element of the group.

Collect! is the method in Ruby Library, let's write a similar method of our own iterate!

Class Array
 def iterate!
  Self.each_with_index do |n, i|
   Self[i] = yield (n) end end

array = [1, 2, 3, 4]

array.iterate! does |n|
 n * * * 2 end

puts Array.inspect

# => [1, 4, 9, 16]

First, we open the array and add the Iterate! method. The method name ends with a dangerous method, which causes attention. Now we may be using iterate! like collect!.

Unlike attributes, you do not need to specify the name of the block in the method, but instead use yield to invoke it. Yield executes the code in the block. Also, notice how we pass N (each_with_index current processing number) to yield. The parameters passed to the yield correspond to the parameters in the block (| | part). Now n can be called by The block and returned N**2 in the yield call.
The entire call is as follows:
1, an array of integers to call iterate!
2, when the yield is called, put N (the first time is 1, the second time is 2, ...) Pass to block
3, block to N for N**2. Because it is the last line, it is returned automatically as a result.
4, yield get the result of block, and rewrite the value into array.
5. Each object in the data performs the same operation.

3, {} and Do...end priority different

The block passed with {} is higher than the priority of using do...end when passing a block;

To avoid ambiguity, it is a good idea to enclose the parameter in the (). For example:

1.upto 3 do |x|

Puts X end



is correct, but 1.upto 3 {|x| puts X} compilation does not pass, should be written as 1.upto (3) {|x| puts X}

Reason:

The block in the 1.upto 3 Do...end is passed to the upto method, and 3 is passed as a parameter to upto

1.upto 3 {|x| puts X} One sentence will be 3 as the function name, pass the block to this function, its return value as the upto method parameters, so compile, but add ().

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.