Home »
Ruby programming
Array.shuffle! Method with Example in Ruby
Ruby Array.shuffle! Method: Here, we are going to learn about the Array.shuffle! method with examples in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on February 09, 2020
Array.shuffle! Method
In this article, we will study about Array.shuffle! method. You all must be thinking the method must be doing something related to the shuffling of elements or objects in the Array instance. It is not as simple as it looks. Well, we will figure this out in the rest of our content. We will try to understand it with the help of syntax and demonstrating program codes.
Method description:
This method is a public instance method and defined for the Array class in Ruby's library. This method works in such a way that it shuffles the objects present in the Array instance randomly. The return type of this method is an Array object which contains all the elements of self in a shuffled manner. You can also provide an optional argument rng which can be used as a random number generator. This method is one of the examples of destructive method which means that the changes created by this method are permanent or non-temporary and would impact the actual arrangement of elements in the self Array instance.
Syntax:
array_instance.shuffle! -> new_array
or
array_instance.shuffle!(random:rng)-> new_array
Argument(s) required:
This method takes one argument which is optional. This argument can be used for random number generation.
Example 1:
=begin
Ruby program to demonstrate shuffle! method
=end
# array declaration
table = [2,4,6,8,10,12,14,16,18,20]
puts "Array shuffle! implementation"
pq =table.shuffle!
puts "Array instance after shuffling: #{pq}"
puts "Array instance:"
print table
Output
RUN 1:
Array shuffle! implementation
Array instance after shuffling: [16, 6, 4, 2, 8, 12, 14, 10, 20, 18]
Array instance:
[16, 6, 4, 2, 8, 12, 14, 10, 20, 18]
RUN 2:
Array shuffle! implementation
Array instance after shuffling: [12, 2, 14, 4, 6, 20, 10, 8, 18, 16]
Array instance:
[12, 2, 14, 4, 6, 20, 10, 8, 18, 16]
Explanation:
In the above code, you can observe that we are shuffling the elements from the Array instance with the help of Array.shuffle! method. You can observe that in both the runs, the output or the Array instance generated is different because the shuffling of the elements is always random. You can also see that the elements in self Array are also changing because this method is one of the examples of the destructive methods.
Example 2:
=begin
Ruby program to demonstrate shuffle! method
=end
# array declaration
table = [2,4,6,8,10,12,14,16,18,20]
puts "Array shuffle! implementation"
pq =table.shuffle!(random: Random.new(2))
puts "Array instance after shuffling: #{pq}"
puts "Array instance:"
print table
Output
RUN 1:
Array shuffle! implementation
Array instance after shuffling: [10, 4, 12, 2, 16, 6, 8, 14, 20, 18]
Array instance:
[10, 4, 12, 2, 16, 6, 8, 14, 20, 18]
RUN 2:
Array shuffle! implementation
Array instance after shuffling: [10, 4, 12, 2, 16, 6, 8, 14, 20, 18]
Array instance:
[10, 4, 12, 2, 16, 6, 8, 14, 20, 18]
Explanation:
In the above code, you can observe that we are shuffling the elements of the Array instance with the help of Array.shuffle method. We are passing an argument inside the method in order to generate a random number. This helps you in the way that it makes the shuffling constant. In both the runs, you can observe that the returning Array is constant. This method is a destructive method that is why it is creating changes in the actual array instance.