From: David Flanagan Date: 2008-08-02T06:43:56+09:00 Subject: [ruby-core:18074] Re: New array methods cycle, choice, shuffle (plus bug in cycle) Wilson Bilkovich wrote: > > I would like to cast my vote (not that I get a vote, but you get the > idea) for Array#sample. > Unlike 'choice', it actually sounds useful to me. > I like 'sample' better than anything I've heard so far. I only like it, however, if it accepts an optional argument for the size of the sample. Picking one random element is so trivial it hardly requires a method. Picking n random elements without allowing duplicates and without duplicating and shuffling the entire array is not trivial and makes this a worthwhile addition to the Array API along with #shuffle. If we agree that there is a use-case for adding #sample to the API, then I would also argue again for #sample! which removes the sampled elements from the array. There is no way to write #sample! based on #sample and I'd imagine that it would be usefule. If the method doesn't take an argument and always returns a single element, then I'd prefer #rand or #random_element (or just removing the method) to #choose or #sample. David Flanagan