From: Jim Weirich Date: 2003-03-25T15:57:54+09:00 Subject: Re: Iterate over two lists in parallel On Tue, 2003-03-25 at 00:25, Hal E. Fulton wrote: > I've been playing with this for about an hour, > and I've only managed to convince myself that > I don't actually understand it. > > What I tried to do was encapsulate this > functionality in a module called Gen, so > that I could, for instance, do this: > > class Array > include Gen > end > > x = [5, 6, 7, 8, 9] > > loop do > a = x.get > break if x.nil? > p a > end > > Yes, I know the loop is trivial and an internal > iterator would suffice here. The point is that > I can't figure out this simple task. :( > > Can you illuminate? I don't know what you have in Gen, but I would expect it to work something like this ... x = [5,6,7,8,9] it = x.iterator loop do a = it.get break if a.nil? p a end A generator is really just a fancy external iterator. So you need an operation to create it (hence the iterator call). You then use the iterator to, ummm, iterator; not the array itself. The iterator method will probably look something like this .... def iterator Generator.new { |g| each { |it| g.generate(it) } } end That's the only thing you would need to mixin. Generator can still stand alone. Does that help? -- -- Jim Weirich jweirich@one.net http://w3.one.net/~jweirich --------------------------------------------------------------------- "Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)