Home » Ruby programming

Array.rrindex() Method with Example in Ruby

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

Array.rindex() Method

In this article, we will study about Array.rindex() method. You all must be thinking the method must be doing something which is related to finding the index of certain elements. 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 a public instance method and defined for the Array class in Ruby's library. This method works in such a way that it returns the index of the object which is passed as an argument in the method but with a twist, it checks from the end of the Array instance. For instance, if there are three "s" in the Array object then it will return the index of the last "s". If a block is provided instead of an argument then it returns the index of the first object for which the block stands to be true starting from the last or end of the Array. This method will return "nil" if no match is found.

Syntax:

    array_instance.rindex(object) -> int, nil
    or
    array_instance.rindex{|item| block} -> int, nil

Argument(s) required:

If this method is not invoked with the block, this method takes one argument which is the name of the object for which the search is going to be held.

Example 1:

=begin
  Ruby program to demonstrate rindex method
=end

# array declaration
lang = ["C++","Java","Java","Java","Python","Html","Javascript","php","Ruby","Kotlin"]

puts "Enter the element you want to search"
ele = gets.chomp

if (lang.rindex(ele))
  puts "#{ele} found at #{lang.rindex(ele)} index"
else
	puts "#{ele} does not exist"
end

Output

Enter the element you want to search 
Java
Java found at 3 index

Explanation:

In the above code, you can observe that we are finding the index of the object with the help of the Array.rindex() method. The object "Java" is present at three different indexes namely 1, 2 and 3 but the method is returning index 3 because in the case of the rindex() method, the search starts from the end of the Array instance.

Example 2:

=begin
  Ruby program to demonstrate rindex method
=end

# array declaration
lang = ["C++","Java","Java","Java","Python","Html","Javascript","php","Ruby","Kotlin"]

puts "Enter the element you want to search"
ele = gets.chomp

if (lang.rindex{|item| item == ele})
  puts "#{ele} found at #{lang.rindex{|item| item == ele}} index"
else
	puts "#{ele} does not exist"
end

Output

Enter the element you want to search 
Java
Java found at 3 index

Explanation:

In the above code you can observe that instead of argument, we are invoking the method with a block. The object "Java" is present at three different indexes namely 1, 2 and 3 but the method is returning index 3 because in the case of rindex() method, the search starts from the end of the Array instance.



Comments and Discussions!

Load comments ↻





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