Home »
Ruby programming
Array.sample() Method with Example in Ruby
Ruby Array.sample() Method: Here, we are going to learn about the Array.sample() method with examples in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on February 04, 2020
Array.sample() Method
In this article, we will study about Array.sample() method. You all must be thinking the method must be doing something which is quite different from all those methods which we have studied. 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 chooses a random object let say r from the elements of Array instance. If you are providing a number 'n' along with the method, then the method will return an Array instance which will be of length 'n' and contains unrepeated and unique random numbers. If the method is invoked without providing any parameter, the method will return 'nil' if the Array instance is found to be empty and an empty Array object is returned if the method is invoked with the argument with the Array which has no elements.
Syntax:
array_instance.sample -> object
or
array_instance.sample(n)-> new_array
Argument(s) required:
This method takes one argument which decides the length of the Array instance which is returned by the method.
Example 1:
=begin
Ruby program to demonstrate sample method
=end
# array declaration
table = [2,4,6,8,10,12,14,16,18,20]
puts "Array sample implementation"
rn = table.sample
puts "The random object generated from Array instance is #{rn}"
Output
RUN 1:
Array sample implementation
The random object generated from Array instance is 8
RUN 2:
Array sample implementation
The random object generated from Array instance is 18
Explanation:
In the above code, you can observe that we are generating random numbers from the Array instance with the help of Array.sample() method. In both the Runs, you can observe that the random numbers being generated are totally different from each other.
Example 2:
=begin
Ruby program to demonstrate sample method
=end
# array declaration
table = [2,4,6,8,10,12,14,16,18,20]
puts "Array sample implementation"
puts "Enter the number of objects you want to generate:"
num = gets.chomp.to_i
rn = table.sample(num)
puts "The random objects generated from Array instance is #{rn}"
Output
RUN 1:
Array sample implementation
Enter the number of objects you want to generate:
3
The random objects generated from Array instance is [20, 18, 2]
RUN 2:
Array sample implementation
Enter the number of objects you want to generate:
3
The random objects generated from Array instance is [2, 6, 8]
Explanation:
In the above code, you can observe that we are generating an Array instance of Random numbers from the Array instance with which Array.sample() method has been invoked. In both the Runs, you can see that the Array instances obtained are totally different from each other.