×

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 - Accessing Array elements

By IncludeHelp Last updated : November 22, 2024

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.

Using Array.take() Method

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.

Syntax

Array.take(index)

Example

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.

Using 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.

Example

Let us understand it with the help of example:

# 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.