From: Daniel Schierbeck Date: 2006-06-20T02:03:22+09:00 Subject: Re: Is it more convenient to have a random_each in ruby stdlib? Matthew Smillie wrote: > 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 Yup, Enumerable's the place it should be. > Though this still relies on Array#shuffle!, which I can't say is a good > or bad thing. Since #shuffle! would alter the receiver, it shouldn't be defined in Enumerable. Other ordered collections could implement their own versions. I was wondering, is Ruby's internal variable "switching" faster than using a temperary variable? #shuffle! can be written like this, but I don't know if it's faster. def shuffle! each_index do |i| r = rand(size - i) + i self[i], self[r] = self[r], self[i] end end Cheers, Daniel