Class: CachingEnumerator
- Inherits:
-
Enumerator
- Object
- Enumerator
- CachingEnumerator
- Defined in:
- lib/caching_enumerator.rb,
lib/caching_enumerator/version.rb
Constant Summary collapse
- VERSION =
"0.0.1"
Instance Method Summary collapse
-
#initialize(&block) ⇒ CachingEnumerator
constructor
A new instance of CachingEnumerator.
- #reset ⇒ Object
Constructor Details
#initialize(&block) ⇒ CachingEnumerator
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/caching_enumerator.rb', line 2 def initialize(&block) @cache = [] @enum = Enumerator.new(&block) super do |yielder| @cache.each do |person| yielder.yield person end begin loop do next_val = @enum.next @cache.push next_val yielder.yield next_val end rescue StopIteration end end end |
Instance Method Details
#reset ⇒ Object
22 23 24 25 26 |
# File 'lib/caching_enumerator.rb', line 22 def reset @cache = [] @enum.rewind self.rewind end |