Home » Ruby programming

Accessing elements of Array with Array.at() method in Ruby

Ruby Array.at() Method: Here, we are going to learn about the Array.at() method with example, we will learn how to access elements of an array in Ruby programming?
Submitted by Hrithik Chandra Prasad, on December 06, 2019

We are very well known with the way to declare an Array in Ruby or you can say that how we can create an instance of Array class? We also know that Ruby has got a wealthy library where you can find various predefined methods and use them as per the requirement of your code. In this tutorial, we will get to learn about one method of Ruby library which is namely Array.at() method. This method is used for accessing or fetching a particular element from the object of the Array class.

With the help of the Array.at() method, you can find any element at a particular index. This method is very helpful in most of the circumstances. In case you want to traverse an array or you can say that you want to print every element of the Array then you can process in the following way:

    Array.at(index)

The above syntax will be more clear to you when you will implement it in code. Let us see how we can implement it in code?

Example 1:

# array declaration
Adc = ['Includehelp.com','Ruby','c++','C#']

# input the index/position
puts "Enter the index of element you want to find"
inx = gets.chomp.to_i

# accessing the element and printing
puts "Element at index #{inx} is #{Adc.at(inx)}"

Output

RUN 1:
Enter the index of element you want to find
1
Element at index 1 is Ruby

RUN 2:
Enter the index of element you want to find
11
Element at index 11 is

Explanation:

You can find in the above code that we are trying to print the element in the Array which is present at a particular index entered by the user. In the Run 1, you can see that when the user has entered the proper index then the value is obtained but in the Run 2, when the user has entered the wrong index there is no output which simply means that this method will not give you error when you are entering an index which is out of the bound.

Example 2:

# array declaration
Adc = ['Includehelp.com','Ruby','c++','C#','java','python']

# limit value 
puts "Enter the limit of traverse"
lm = gets.chomp.to_i

# loop to print index with element
i = 0
for i in 0..lm
  puts "Element at index #{i} is #{Adc.at(i)}"
end

Output

RUN 1:
Enter the index of element you want to find
5
Element at index 0 is Includehelp.com
Element at index 1 is Ruby
Element at index 2 is c++
Element at index 3 is c#
Element at index 4 is java
Element at index 5 is python

RUN 2:
Enter the index of element you want to find
8
Element at index 0 is Includehelp.com
Element at index 1 is Ruby
Element at index 2 is c++
Element at index 3 is c#
Element at index 4 is java
Element at index 5 is python
Element at index 6 is
Element at index 7 is
Element at index 8 is

Explanation:

In the above code, you can observe the Array.at() method can also be used to traverse the Array. Here, we are taking a limit from the user in the form of entered value from the console and traversing the Array as per the demand of the user. You can also observe that the program is not giving any kind of Exception when the limit is exceeding the total number of elements present in the Array.



Comments and Discussions!

Load comments ↻





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