< 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