Home » Ruby programming

Array.each_index Method with Example in Ruby

Ruby Array.each_index Method: Here, we are going to learn about the Array.each_index method with examples in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on January 26, 2020

Array.each_index Method

In this article, we will learn about Array.each_index method. We have already discussed the Array.each method. You must be little more excited to read about Array.each_index method due to its catchy name as I was pretty amazed after reading this method. In the upcoming content, we will see the way through which we can implement this method. We will understand it with the help of its syntax and demonstrating examples.

Method description:

This method is specially defined for the Array class in Ruby's library. This method is one of the examples of Array instance methods. It works like a loop and calls or invokes the given block for each element of Array instance for a definite number of times. This method is different from Array.each method in the way that instead of passing the element, it requires the index of the elements. This method will return an enumerator if no block is given. This method does not undergo any change in the actual elements of Array objects. This method can be considered as one of the examples of non-destructive methods present in the Rich Ruby library.

Syntax:

    each_index { |index| block }

Parameter(s) required:

This method does not require any parameter and does not accept any argument.

Example 1:

=begin
  Ruby program to demonstrate each_index method
=end

# array declaration
Array1 = ["Kumar","Ramesh","Apple","Pappu","Sana","Yogita","Satyam","Harish"]

puts "Array each_index implementation."
Array1.each_index{|x| print x, "--"}

Output

Array each_index implementation.
0--1--2--3--4--5--6--7--

Explanation:

In the above code, you will observe that we are processing the Array instance with the help of Array.each_index method but here we are not getting the elements but we are getting the indexes of those elements. This is how each_index method works?

Example 2:

=begin
  Ruby program to demonstrate each_index method
=end

# array declaration
Array2 = [456,344,222,444,55,5,900]

puts "Array each_index implementation."
Array2.each_index{|x| print x, "--"}

Output

Array each_index implementation.
0--1--2--3--4--5--6--

Explanation:

In the above code, you can observe that this method works for Integer Array instances as well. Here also, it is providing the indexes of all integer elements available inside the Array instance.







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.