From: Lars Haugseth Date: 2012-06-27T00:01:05+09:00 Subject: Re: Array#sample is Set#sample and not Array#sample ! On 06/26/2012 04:50 PM, Josh Cheek wrote: > On Tue, Jun 26, 2012 at 4:48 AM, Lars Haugseth > > wrote: > > I agree, sample isn't intended to work the way the OP suggests. > > There are other ways to achieve the desired effect: > > class Array > def ordered_sample(n) > values_at [*0...size].sample(n).sort > end > end > > a = [:a, :b, :c, :d, :e, :f, :g, :h] > > a.ordered_sample(4) > => [:b, :c, :d, :e] > a.ordered_sample(4) > => [:c, :d, :e, :h] > a.ordered_sample(4) > => [:a, :b, :c, :g] > > Maybe we're on different rubies, but I had to change the implementation > to `values_at *[*0...size].sample(n).sort` You're right, I did a copy&paste error and forgot to test my latest edit to the email. A bit more readable: values_at size.times.to_a.sample(n).sort -- Lars Haugseth