How to shuffle an array in ruby?
Array#shuffle
method
The shuffle
method will shuffle the elements in an array and returns a new array.
array = [1, 2, 3, 4, 5]
array.shuffle
# => [3, 4, 2, 5, 1]
array
# => [1, 2, 3, 4, 5]
Array#shuffle!
method
The shuffle!
method will shuffle the elements in an array in place.
array = [1, 2, 3, 4, 5]
array.shuffle!
# => [3, 4, 2, 5, 1]
array
# => [3, 4, 2, 5, 1]