On Feb 9, 2007, at 6:23 PM, Garance A Drosehn wrote:
> I should also say that I am not looking for a quick-fix for some
> specific script that I am writing.  This is more of a "blue-sky"  
> topic,
> where I am thinking that maybe some future version of ruby could
> introduce a new feature for these situations.

I think that using an array as a queue for processing items is
pretty general. The idea of pushing things back into a queue is
also a reasonably common pattern.  You suggested that you didn't
want to change your array during iteration, but if you simply
think of the array as a queue of items then it certainly makes
sense to alter the queue as you process items.  You can always dup
the original array if you need continued access to the original
collection.

I gave an illustration with ARGV but the pattern works for any type
of a collection.  You can always use Enumerable#to_a to convert
something to an array:

hash = { 1 => 2, 3=>4 }
pairs = hash.to_a

while pair = pairs.shift
   # the next pair is available as pairs[0], if necessary
end

Is there some particular reason you prefer iteration via #each over
a while loop?  It seems like the problem you described is more general
than the type of iteration provided by #each and so it needs a more
general solution (like a while loop).  As you've said, you've managed
to shoehorn in a solution with #each via flags and such, but
perhaps that indicates that #each is simply the wrong pattern for your
use cases.

It isn't entirely clear to me from the single example what 'pattern'
you are trying to implement. Perhaps posting another use
case would generate some more suggestions.


Gary Wright