Home » Ruby programming

Array.rassoc() Method with Example in Ruby

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

Array.rassoc() Method

In this article, we will study about Array.rassoc() method. You all must be thinking the method must be doing something which is quite different from all those methods which we have studied. 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 belongs to the Array class which lives inside the library of Ruby language. This method is used to check whether an object is a part of the particular Array instance or not and that Array instance cannot be a normal Array instance. If it is not normal, it means that Array instance is the Array of multiple Array instances or you can say that it the collection of multiple objects which are itself an object of Array class. It works for the Array instances whose elements are also Array instances. Let us go through the syntax and demonstrating the program codes of this method.

If you are thinking about what it will return then let me tell you, it will return the first contained Array instance where it found the presence of the object. It will return "nil" if it hadn't found the object in any of the Arrays.

Syntax:

    array_instance.rassoc(obj)

Argument(s) required:

This method only takes one parameter and that argument is nothing but an object whose presence we want to check.

Example 1:

=begin
  Ruby program to demonstrate rassoc method
=end

# array declarations
array1 = [1,"one"]
array2 = [2,"two"]
array3 = [3,"three"]

array_main = [array1,array2,array3]

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

if array_main.rassoc(ele) != nil
	puts "Element found in:"
	print  array_main.rassoc(ele)
else
	puts "Element not found"
end

Output

RUN 1:
Enter the element you want to search
 two
Element found in:
[2, "two"]

RUN 2:
Enter the element you want to search
 one
Element found in:
[1, "one"]

Explanation:

In the above code, you can find that the Array instance on which we have invoked rassoc() method is not any normal Array instance. It is the collection of multiple Array instances. It is returning the whole Array instance where it has found the object inputted by the user.

Example 2:

=begin
  Ruby program to demonstrate rassoc method
=end

# array declaration
array1 = ["Babita","Sabita","Ashok"]
puts array1.rassoc("Babita")

Output

No output

Explanation:

In the above you can verify that rassoc() method does not works upon normal Array instances. It will return nil even if the object is a part of 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.