Home » Ruby programming

Array.take() Method with Example in Ruby

Ruby Array.take() Method: Here, we are going to learn about the Array.take() Method with examples in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on February 12, 2020

Array.take() Method

In this article, we will study about Array.take() Method. You all must be thinking the method must be doing something related to taking elements or objects from 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 a way that takes n number of arguments from the Array instance and returns a new Array instance that contains the objects which are being taken from the self Array. The method will throw an exception if you provide a negative value along with the method at the time of its invocation. This method is one of the examples of the non-destructive method where the method does not bring any changes in the actual arrangement of objects in the self Array.

Syntax:

    array_instance.take(n) -> new_array or nil

Argument(s) required:

This method requires only one argument which decides the number of objects to be taken from the Array instance. The argument strictly needs to be a positive integer otherwise the method will throw an exception.

Example 1:

=begin
  Ruby program to demonstrate take method
=end

# array declaration
table = [2,4,6,8,10,12,14,16,18,20]

puts "Array take implementation"

puts "Enter the number of objects you want to take from the Array:"
num = gets.chomp.to_i

if(table.take(num))
	puts "The objects taken are: #{table.take(num)}"
else
	puts "Exception occured"
end

puts "Array instance: #{table}"

Output

RUN 1:
Array take implementation
Enter the number of objects you want to take from the Array:
 4
The objects taken are: [2, 4, 6, 8]
Array instance: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

RUN 2:
Array take implementation
Enter the number of objects you want to take from the Array:
 -5
attempt to take negative size
(repl):13:in `take'
(repl):13:in `<main>'

Explanation:

In the above code, you can observe that we are taking the elements from the Array instance with the help of the Array.take() method. The taking of objects or elements will start from the very first index of the Array instance which is always 0. In the second output, you can observe that when we are providing a negative index, the method is throwing an exception because in the case of this method there is nothing like objects can be taken from the end of the Array instance.

Example 2:

=begin
  Ruby program to demonstrate take method
=end

# array declaration
table = ["Subha","Sham","Raat","Vivek","Me"]

puts "Array take implementation"

puts "Enter the number of objects you want to take from the Array:"
num = gets.chomp.to_i

if(table.take(num))
	puts "The objects taken are: #{table.take(num)}"
else
	puts "Exception occured"
end

puts "Array instance: #{table}"

Output

RUN 1:
Array take implementation
Enter the number of objects you want to take from the Array:
 3
The objects taken are: ["Subha", "Sham", "Raat"]
Array instance: ["Subha", "Sham", "Raat", "Vivek", "Me"]

RUN 2:
Array take implementation
Enter the number of objects you want to take from the Array:
 -9
attempt to take negative size
(repl):13:in `take'
(repl):13:in `<main>'

Explanation:

This program is demonstrated just to show you that this method works on String Array as well. In the above code, you can observe that we are taking the elements from the Array instance with the help of the Array.take() method. The taking of objects or elements will start from the very first index of the Array instance which is always 0. In the second output, you can observe that when we are providing a negative index, the method is throwing an exception because in the case of this method there is nothing like objects can be taken from the end of the Array instance.



Comments and Discussions!

Load comments ↻





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