You can use the .sample method in combination with the Array#length method to randomly select a sample of 5 unique elements from an array without replacement:
main.rb74 chars3 lines
This will return a new array of 5 randomly chosen, non-repeating elements from the original array. If the original array has less than 5 elements, .sample will still return a shuffled version of the original array, but with no repeats. If the original array is empty, .sample will return an empty array.
gistlibby LogSnag