Module: BLazy
- Defined in:
- lib/b-lazy.rb
Class Method Summary collapse
-
.when_needed(&f) ⇒ Object
Occassionally, we find that it is necessary to compute all values upfront before the enumeration.
Class Method Details
.when_needed(&f) ⇒ Object
Occassionally, we find that it is necessary to compute all values upfront before the enumeration. For example, if we are extracting lines from a file, then we might decide that guaranteeing that the file is closed cleanly is more important than lazily enumerating.
This function serves as a compromise: compute all of the values, but only do so if I attempt to enumerate over them.
367 368 369 370 371 372 |
# File 'lib/b-lazy.rb', line 367 def when_needed(&f) Enumerator.new do |out| values = f.call values.each{|v| out.yield v} end end |