I have been using Generator in 1.8, and now I need to port that code to
1.9. My code looks essentially like the code below. I'm confused as to
what the correct option is in 1.9: Fibers? Enumerator?
Enumerator::generator? I'd like to know the simplest most
straightforward option possible, since I'm still a relatively novice
Ruby practitioner.
Any help gratefully received!
require 'generator'
def bar
Generator.new do |g|
(1..10).each do |number|
g.yield number
end
end
end
However, in your case you don't even need to explicitly create an
Enumerator, because this will be done automatically when omitting the
block for the "each" method:
Why do you you even want an external iterator? Because what you've wrote
down is an *internal* iterator created from an external iterator created
from an internal iterator (which is a lot of useless code).