To shuffle an array in Ruby, you can use the shuffle method, which is a built-in method of the Array class. Here's an example:
main.rb89 chars4 lines
This will output an shuffled version of the array my_array.
Alternatively, you can shuffle an array in place using the shuffle! method:
main.rb40 chars3 lines
This will modify the original array my_array and shuffle its elements randomly.
Another way is to use the sort_by method with a random number generator:
main.rb26 chars2 lines
This will return a shuffled version of the array my_array. Note that this method doesn't modify the original array, it returns a new shuffled array.
gistlibby LogSnag