Home » Ruby programming

Array.index() Method with Example in Ruby

Ruby Array.index() Method: Here, we are going to learn about the Array.index() method with examples in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on February 04, 2020

Array.index() Method

In this article, we will study about Array.index() method. You all must be thinking the method must be doing something which is related index of certain element. It is not as simple as it looks. Well, we will figure this out in the rest of our content. We will try to understand it with the help of syntax and demonstrating program codes.

Method description:

This method is one of the examples of the Public instance method which is specially defined in the Ruby library for Array class. This method is used to find the index of a certain element of the Array instance with the help of the element being passed as the argument. This method works in the way that it returns the index of the first object in Array instance. If you are providing a block instead of an argument then the method will return the index of the first object for which the block returns a Boolean value "True" and returns "nil" if no match is found. This method is one of the examples of non-destructive methods where the changes made by these methods are permanent. There is no destructive version of this method.

Syntax:

    array_instance.index()
    or
    array_instance.index(|item| block)

Argument(s) required:

This method accepts an argument which should be present in the Array instance. This method returns nil if that element is not present in the Array instance.

Example 1:

=begin
  Ruby program to demonstrate index method
=end	

# array declaration
lang = ["C++","Java","Python","Ruby","Perl"]

puts "Array index implementation."
puts "Enter the element whose index you want to find:"
ele = gets.chomp

if(ind = lang.index(ele))
	puts "#{ele} found at index #{ind}"
else
	puts "element is not a part of the Array instance"
end

Output

Array index implementation.
Enter the element whose index you want to find:
 Ruby
Ruby found at index 3

Explanation:

In the above code, you can observe that we are asking the user for the element whose index she wants to find. The user has entered the object "Ruby" which is present in the 3rd index of the Array instance.

Example 2:

=begin
  Ruby program to demonstrate index method
=end	

# array declaration
lang = ["C++","Java","Python","Ruby","Perl"]

puts "Array index implementation."
puts "Enter the element whose index you want to find:"
ele = gets.chomp

if(ind = lang.index{|ind| ind == ele})
	puts "#{ele} found at index #{ind}"
else
	puts "element is not a part of the Array instance"
end

Output

Array index implementation.
Enter the element whose index you want to find:
 Python
Python found at index 2

Explanation:

In the above code, you can see that we are passing a block inside the method. The method is returning the index of the first element for which the block stands to be True.




Comments and Discussions!

Load comments ↻






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