| Class | Enumerator |
| In: |
lib/more/facets/enumerator.rb
|
| Parent: | Object |
| initialize | -> | old_initialize |
Provides the ruby-1.9 block form of Enumerator, where you can write:
obj = Enumerator.new do |yielder|
.. do stuff
yielder.yield data # or: yielder << data
.. etc
end
When obj.each is called, the block is run once. It should call yielder.yield with each item it wishes to generate.
Example:
fib = Enumerator.new { |y|
a = b = 1
loop {
y << a
a, b = b, a + b
}
}
assert_equal [1, 1, 2, 3, 5, 8, 13, 21, 34, 55], fib.take(10)
# File lib/more/facets/enumerator.rb, line 35
35: def initialize(*args, &block)
36: if block_given?
37: @body = block
38: old_initialize(self, :_start)
39: else
40: old_initialize(*args)
41: end
42: end