Home » Ruby programming

Array.assoc(obj) Method with Example in Ruby

Ruby Array.assoc(obj) Method: Here, we are going to learn about the Array.assoc(obj) method with example in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on January 06, 2020

Ruby Array.assoc(obj) Method

In the previous articles, we have seen how we can check whether two Array instances are identical or not with the help of <=> operator, == operator, and .eql? method? We have also seen different ways through which we can insert elements in the previously defined Array instances. Now, we can say that we have got a decent amount of knowledge about the Array class in Ruby language. In this article, we will see how we can implement the Array.assoc() method? We will go through its syntax and some examples in the rest of the Array.

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. 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.




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

Example 1:

  Ruby program to demonstrate assoc method

# arrays
array1 = [1,"Ramesh","Apple",12,true,nil,"Satyam","Harish"]
array2 = ["Akul","Madhu","Ashok","Mukesh",788]
array3 = ["Orange","Banana","Papaya","Apricot","Grapes"]

# main array 
arraymain = [array1,array2,array3]

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

# checking
if arraymain.assoc(ele) != nil
	puts "Element found in:"
	print  arraymain.assoc(ele)
	puts "Element not found"


RUN 1:
Enter the element you want to search
Element found in:

RUN 2:
Enter the element you want to search
Element not found


In the above code, you can find that the Array instance on which we have invoked assoc() 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:

  Ruby program to demonstrate assoc method

# array
array1 = ["Babita","Sabita","Ashok"]

# checking 
puts array1.assoc("Babita")




In the above, you can verify that assoc() method does not work upon normal Array instances. It will return nil even if the object is a part of the Array instance.



Comments and Discussions



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

© some rights reserved.