How to randomly sort (scramble) an array in Ruby?

Built in now:

[1,2,3,4].shuffle => [2, 1, 3, 4]
[1,2,3,4].shuffle => [1, 3, 2, 4]

For ruby 1.8.6 (which does not have shuffle built in):

array.sort_by { rand }

For ruby 1.8.6 as sepp2k's example, but you still want use "shuffle" method.

class Array
  def shuffle
    sort_by { rand }
  end
end

[1,2,3,4].shuffle #=> [2,4,3,1]
[1,2,3,4].shuffle #=> [4,2,1,3]

cheers