On Fri, 29 Dec 2000, Jim Menard wrote:

> I don't know if each, as defined by classes that mixin Enumerable, should
> always return values in the same order. I have never relied on that
> behavior.

I'll bet you have -- class Array mixes in Enumerable, and defines
#each, so every time you #each through an array and expect it to be in
the same order, you're relying on that.

In some cases, one might rely on the enumerated elements *not* being
in the same order:

   class DeckOfCards

     include Enumerable

     def initialize(arr)
       @arr = arr
     end

     def each
       a = @arr.dup
       yield a.delete_at(rand a.size) until a.empty?
     end

   end

   DeckOfCards.new( %w{ ace two three four jack queen king } ).each \
     do |c| puts c end


(No flames about the shuffling algorithm, please -- I just wanted it
to be visually compact.)


David

-- 
David Alan Black
home: dblack / candle.superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav