Sliced Software

About

Thomas Mango writes software, has a bulldog, listens to Long Island Hardcore music and eats sandwiches.

He is currently working on Firefly, The Check-in Service for Twitter and Limited Pressing, An Online Store Community.

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.

>> [0, 1, 2, 3, 4].rand
=> 2
This rand plugin overrides the 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.
>> [0, 1, 2, 3, 4].rand(2)
=> [4, 0]

>> [0, 1, 2, 3, 4].rand(4)
=> [3, 0, 2, 1]
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.

Looking for really old posts? Until they are here at tumblr, see here!