ADVERTISEMENT
ADVERTISEMENT

Ruby program to demonstrate the assoc() method

Ruby Example: Write a program to demonstrate the assoc() method.
Submitted by Nidhi, on January 12, 2022

Problem Solution:

In this program, we will use the assoc() method to get the array from an array of array-based on the specified first element.

Program/Source Code:

The source code to demonstrate the assoc() method is given below. The given program is compiled and executed successfully.

# Ruby program to demonstrate the 
# assoc() method

arr1 = ["GODs","RAM","KRISHNA","SHIV"];
arr2 = ["COMPANY","HCL","TCS","WIPRO"];
arr3 = ["CITY", "DELHI", "MUMBAI", "AGRA"];

ArrayOfArray = [arr1,arr3,arr2];

X = ArrayOfArray.assoc("CITY")
Y = ArrayOfArray.assoc("GODs")
Z = ArrayOfArray.assoc("COMPANY")

print "#{X}\n";
print "#{Y}\n";
print "#{Z}\n";

Output:

["CITY", "DELHI", "MUMBAI", "AGRA"]
["GODs", "RAM", "KRISHNA", "SHIV"]
["COMPANY", "HCL", "TCS", "WIPRO"]

Explanation:

In the above program, we created 3 arrays arr1, arr2, arr3. Then we created an array of arrays. After that, we used the assoc() method to get the array based on the specified 1st element and printed the resulting arrays.

Ruby Arrays Programs »


ADVERTISEMENT


ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

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.