Created
August 31, 2009 04:22
-
-
Save raws/178275 to your computer and use it in GitHub Desktop.
An instance method for Ruby's Array class which picks a number of elements from the array at random
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Array | |
# Pick +number+ random elements out of the array. If +number+ is | |
# greater than the size of the array, this method will simply return | |
# the array itself, sorted randomly. | |
def pick(number) | |
sort_by { rand }.slice(0...number) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment