Skip to content

Instantly share code, notes, and snippets.

@raws
Created August 31, 2009 04:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raws/178275 to your computer and use it in GitHub Desktop.
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
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