Home » Ruby programming

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

Ruby Array.fetch() Method: Here, we are going to learn about the Array.fetch() method with example, we will learn how to access elements of 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 the Array.fetch() method. This method is used for accessing or fetching a particular element from the object of the Array class. We will also see how it is different from the Array.at() method?

With the help of the Array.fetch() method you can find any element at a particular index. This method is very helpful in most of the circumstances. It is different from Array.at() method in the way that the Interpreter will give an error if the traversing or searching goes out of bounds. If 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.fetch(index)

The above syntax will be clearer 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#','php']

# 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.fetch(inx)}"

Output

RUN 1:
Enter the index of element you want to find 
2
Element at index 2 is c++

RUN 2:
Enter the index of element you want to find
 34
index 34 outside of array bounds: -5...5
(repl):9:in `fetch'
(repl):9:in `<main>'

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 or the index which is greater than the length of the Array then Interpreter will give you an error.

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.fetch(i)}"
end

Output

RUN 2:
Enter the limit of traverse
 4
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

RUN 2:
Enter the limit of traverse
 23
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
index 6 outside of array bounds: -6...6
(repl):11:in `fetch'
(repl):11:in `block in <main>'
(repl):10:in `each'
(repl):10:in `<main>'

Explanation:

In the above code, you can observe the Array.fetch() 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. In Run 1, the execution went well because the limit was less than the length of the Array but in Run 2, the interpreter gave an error because the limit was more than the count. In this way Array.fetch() is different from Array.at() method.







Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.