Home » Ruby programming

Accessing Array elements using Array.take() and Array.drop() methods in Ruby

Ruby Array.take() and Array.drop() methods: Here, we are going to learn how to access array elements using Array.take() and Array.drop() methods in Ruby programming language?
Submitted by Hrithik Chandra Prasad, on December 14, 2019

Ruby Array.take() and Array.drop() methods

Before reading this article, you must be very aware of the basics of Array in Ruby. You should know how to declare an Array. We know that when we write the name of Array along with puts statement, it returns all the Array elements in the following way

# declaring an array 
Adc = ['Includehelp.com','Ruby','C++','C#','Java','Python']

# printing array elements 
puts Adc

Output

Includehelp.com
Ruby
C++
C#
Java
Python

In the above output, you can observe that when we are printing the instance of Array class along with the "puts" statement, it is providing me all the elements present in the Array. But what if we want only certain elements at a point of time. We must be having some methods for it. Ruby never fails to provide you variety in its methods. To serve this purpose, we have methods like Array.take() and Array.drop(). They both provide you elements as per your requirements. Let us understand the implementation of both the methods with the help of their syntaxes and their examples which are given below.

Array.take() Method

Syntax:

    Array.take(index)

This method returns the first n elements of the object of Array class. For theoretical demonstration, if you are providing 3 in the argument list of the method then it will return you elements up to index 3. You will get your concept cleared most effectively with the help of example given below,

# Array declaration
Adc = ['Includehelp.com','Ruby','C++','C#','Java','Python']

# input the range 
puts "Enter the index upto 5"
ind = gets.chomp.to_i

# printing the elements till the range
if ind<Adc.count
  puts "The elements are #{Adc.take(ind)}"
else
  puts "Incorrect index"
end

Output

RUN 1:
Enter the index upto 5
 4
The elements are ["Includehelp.com", "Ruby", "C++", "C#"]

RUN 2:
Enter the index upto 5
 10
Incorrect index

Explanation:

In the above code and output, you can observe that when the user has input 3 as the index. It is getting passed inside the Array.take() as the argument. It is printing the elements up to that index i.e 3. It will print the "Incorrect index" if the index is more than the elements present in the Array.

Array.drop() Method

As the name suggests, the drop is the opposite of taking. It works as it returns the elements after n elements have been dropped. Remember, drop does not delete the elements. Let us understand it with the help of examples and syntax.

# Array declaration
Adc = ['Includehelp.com','Ruby','C++','C#','Java','Python']

# input the range 
puts "Enter the index upto 5"
ind = gets.chomp.to_i

# printing the elements after dropping given
# number of elements
if ind<Adc.count
  puts "The elements are #{Adc.drop(ind)}"
else
	puts "Incorrect index"
end

# printing all elements of the array 
puts "Array elements are:"
puts Adc

Output

Enter the index upto 5
 4
The elements are ["Java", "Python"]
Array elements are:
Includehelp.com
Ruby
C++
C#
Java
Python

Explanation:

In the above code and output, you can observe that the method is returning after the limit or argument provided by the user. You can also observe that we are printing the Array elements afterward are Array.drop() is not creating any impact on the Array elements in any way.



Comments and Discussions!

Load comments ↻





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