From: Lars Haugseth Date: 2012-06-26T18:48:13+09:00 Subject: Re: Array#sample is Set#sample and not Array#sample ! On 06/25/2012 11:03 PM, Ryan Davis wrote: > > On Jun 25, 2012, at 09:55 , Bartosz Dziewo��ski wrote: > >> 2012/6/25 Robert Klemme : >>> I am sorry, I don't get it. Can you explain what your expectation is? >> >> I think he wants to keep the ordering or original array in the result >> of #sample. >> >> This might actually make sense. > > So you would expect `(1..3).to_a.sample(3)` to always output [1, 2, 3] ? > > That doesn't make sense to me. 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] -- Lars Haugseth