×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Ruby Array.sample() Method

By IncludeHelp Last updated : December 01, 2024

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.

Description and Usage

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

Parameters

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.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.