From: Matthew Smillie Date: 2006-06-20T01:36:29+09:00 Subject: Re: Is it more convenient to have a random_each in ruby stdlib? > I'm not too keen on #shuffle having a dual function as both a > shuffler and an iterator -- I'd be more comfortable using > `ary.shuffle.each'. That would also allow a major simplification of > the implementation: > > class Array > def shuffle > values_at(*(0..size-1).to_a.shuffle!) > end > end I agree with you about 'arr.shuffle.each' being better semantically, but I was worried about efficiency; since the thread started out with concerns about shuffling really big arrays, I wanted to avoid creating an intermediate copy of the array. I didn't really think about it that hard, though, because thinking about it now, I realise an intermediate copy isn't all that bad (being just references), so I'm not sure the savings would matter all that much. Anyway, Array's just a special case: I'd say that it belongs in Enumerable along with sort, for which I'd grab your implementation. module Enumerable def shuffle entries.values_at(*(0...entries.size).entries.shuffle!) end end Though this still relies on Array#shuffle!, which I can't say is a good or bad thing. Bit of a moot question unless someone does decide to put it into core or stdlib, though. matthew smillie.