I needed to randomly sort the elements in an array in the Ruby on Rails project I'm working on.
Turns out these things are just way too easy in Ruby:
Credits to Tom Klaasen for improving the more complex snippet I found before.
Or you can use the rand method when you only need one single element and don't need to shuffle the whole array in Ruby 1.8:
And for Ruby 1.9:
This post is open source. Did you spot a mistake? Ideas for improvements? Contribute to this post via Github. Thank you!