A better Array#rand
I released a new plugin today, called rand. This plugin expands on the #rand method ActiveSupport adds to Array.
As background, the rand method that Rails gives you, returns a single random value back from the array it’s called on.
This rand plugin overrides the
>> [0, 1, 2, 3, 4].rand
=> 2
rand method so that you can pass in an integer value that corresponds to how many random values from the array you actually want back.
Additionally, the existing functionality is still in place so that if you don’t pass in a parameter, it still gives you back a single random value.
>> [0, 1, 2, 3, 4].rand(2)
=> [4, 0]
>> [0, 1, 2, 3, 4].rand(4)
=> [3, 0, 2, 1]