Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Duane Johnson http://blog.inquirylabs.com/

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Array#randomly_pick( n )

Adds a handy function that lets you randomly pick n elements from an array.

class Array
  # If +number+ is greater than the size of the array, the method
  # will simply return the array itself sorted randomly
  def randomly_pick(number)
    sort_by{ rand }.slice(0...number)
  end
end
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS