From: Daniel Schierbeck Date: 2006-06-20T04:10:57+09:00 Subject: Re: Is it more convenient to have a random_each in ruby stdlib? Matthew Smillie wrote: >> Since #shuffle! would alter the receiver, it shouldn't be defined in >> Enumerable. Other ordered collections could implement their own versions. > I agree with you there, but it's not quite what I meant. More clearly, > Enumerable#entries returns an array with no guarantees about the > ordering. Enumerable#sort and #shuffle return similar arrays, but > include guarantees about the ordering. Enumerable#shuffle uses > Array#shuffle! internally to produce the final array, which just doesn't > quite click for me. It's just an aesthetic disagreement, and I can't > quite put my finger on anything concrete. I can see what you mean -- you'd rather have #shuffle! depend on #shuffle than the other way round? I.e. class Array def shuffle! replace(shuffle) end end I guess that could be possible. module Enumerable def shuffle ary = entries ary.each_index do |i| r = rand(ary.size - 1) + i ary[i], ary[r] = ary[r], ary[i] end end end Of course, it would not be much different than `entries.shuffle!' Cheers, Daniel