and Generator is now a class inside Enumerator. An Enumerator::Generator
is created automatically, for the use of the enumerator, if you create
an enumerator with a block. Also, these Generators don't have #next and
friends; that's available via the enumerator. (I'm not sure what
happened to #pos.)
As I understand it, the main thing about generators is that "controlled
stream" thing, where you can roll your own sense of iteration, rather
than just piggy-backing on what some enumerable object with its own
ideas about iteration thinks. Here's a (very contrived) 1.9 example:
[dblack@ruby-versions ~]$ cat e.rb message =3D nil
g =3D Enumerator::Generator.new do |yielder|
=A0yielder << "Hi."
=A0puts "I've been told to #{message}."
=A0case message
=A0when "leave"
=A0 =A0yielder << "Bye."
=A0when "stay"
=A0 =A0yielder << "I'm still here!"
=A0end
end
e =3D Enumerator.new(g)
puts e.next
message =3D "stay"
puts e.next
e.rewind
puts e.next
message =3D "leave"
puts e.next
[dblack@ruby-versions ~]$ ruby e.rb Hi.
I've been told to stay.
I'm still here!
Hi.
I've been told to leave.
Bye.
David
--
David A. Black, Senior Developer, Cyrus Innovation Inc.
=A0The =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Ruby training with Black/Brown= /McAnally
=A0Compleat =A0 =A0 =A0 =A0 =A0 =A0 =A0Philadelphia, PA, October 1-2, 201= 0
=A0Rubyist =A0 =A0 =A0 =A0 =A0 =A0 =A0
http://www.compleatrubyist.com