Inject in ruby

Source: Internet
Author: User

When I first came into contact with inject, I didn't know much about it. I just saw this article.ArticleRuby-inject, To get a new understanding, the article with a large number of examples to illustrate.
1. Let's look at the simplest example of summation. Here we will analyze the implementation mechanism of inject.

Ruby Code
    1. [1, 2, 3, 4]. Inject (0) {| result, element | result + element}#=> 10


Inject has a parameter and a block. The two parameters in the block have meanings. The first parameter reslut pays the value of the inject parameter when the inject executes the block for the first time. element is the element in the array. In this example, the inject executes the block four times in total, after each block is executed, the result of the final statement is paid to the result, so that the loop continues until all elements in the array are traversed. Let's take a deeper look at the results of each step.
Because the array has four elements, You need to perform four block operations:
First operation: Result = 0, which is equal to the inject parameter; element = 1, which is the first element; Result + element = 1 after the block is executed, pay the result to result, so after the first block is executed, result = 1.
The second operation: Result = 1, indicating the result of the last statement after the block was last run; element = 2, indicating the second element; Result = 3 after the block is executed.
And so on, until the last block is executed, the production value is 10. Is the return value of inject.

Inject can be left blank. The initial value of result is the first value of the array element. Therefore, the preceding example can be changed:

Ruby code
    1. [1, 2, 3, 4]. Inject {| result, element | result + element}


At this time, the number of blocks executed is three times.

The article also mentions other usage:

Ruby code
    1. Hash = [[: First_name,'Shares'], [: Last_name,'Harvi']. Inject ({})Do| Result, element |
    2. Result [element. First] = element. Last
    3. Result
    4. End
    5. Hash#=>{: First_name => "Shane",: last_name => "Harvie "}


Converts an array to a hash table. There is also the basis for the first example, which should be easily understood.

Let's look at another example. We need to filter and process an array of elements,

Ruby code
    1. [1, 2, 3, 4, 5, 6]. Select {| element % 2 = 0}. Collect {| element. to_s}# => ["2", "4", "6"]


The code above selects the even numbers in the array and converts them to the struct type. How can I implement it with inject?

Ruby code
    1. Array = [1, 2, 3, 4, 5, 6]. Inject ([])Do| Result, element |
    2. Result <element. to_sIfElement % 2 = 0
    3. Result
    4. End
    5. Array# => ["2", "4", "6"]


Is it more intuitive?

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.