From: Yusuke ENDOH Date: 2011-01-11T22:39:22+09:00 Subject: [ruby-core:34351] Re: [Ruby 1.9-Feature#4247][Open] New features for Array#sample, Array#choice Hi, 2011/1/8 Benoit Daloze : > Is there a reason to change Array#sample instead of using the > enumerator form with take(n) to get an Array of n random elements ? > > I find weird to have a method (#sample) returning a single element or > an Array depending on parameters. You have a misunderstanding. Array#sample already supports optional argument to specify the count of elements sampled. This is not a new behavior. $ ruby -ve 'p [1, 2, 3].sample(2)' ruby 1.9.2p0 (2010-08-18 revision 29036) [i686-linux] [2, 1] Unfortunately, this behavior is included in 1.9.2 which is already released. It can no longer change. > So here is my proposition: > Array#sample remains unchanged. > Array#samples is like the proposed #each_sample, with the option > :repeat to use replacement. Though I'm not against your proposition, it may be a bit confusing because Array#sample may also return some elements. Also, does anyone have an opinion about the keyword :repeat which allows duplicated samples? Personally, I don't hate. p [1, 2, 3].sample(5, repeat: true) #=> [2, 2, 3, 1, 3] -- Yusuke Endoh