Ruby Enumerator::lazy

Source: Internet
Author: User

< Span class= "S1" > < span class= "n" > < Span class= "o" >  

when a large array or collection needs to be looped, it can be a big disadvantage to put the data into memory at once. This is where lazy comes in handy.
/span>
Float::INFINITY 是无穷大意思
举个例子 取出1到无穷大对7整除余数为0的前10个数值。
(1..float::infinity). Select {|x| x% 7 = = 0}.take (ten). to_a

This writing will directly die, this time with lazy

(1..float::infinity). lazy.select {|x| x% 7 = = 0}.take (ten). to_a

Similarly, when we operate a very large file, the direct each_line operation will put the entire file into memory.

File = File.Open ('xxxx'r' |row| row.xxx}

The lazy method will load the file incrementally.

File = File.Open ('xxxx'r' |row| row}

  

Detailed usage of Enumerator::lazy:

https://railsware.com/blog/2012/03/13/ruby-2-0-enumerablelazy/

< Span class= "NF" > < span class= "n" > < Span class= "NF" >  


/span>

Ruby Enumerator::lazy

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.