Home » Ruby programming

Ruby assoc() function

Ruby assoc() function: Here, we are going to learn about the assoc() function with example in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on September 01, 2019

assoc() function in Ruby

We have studied functions to process single dimensional array so far but if we talk about assoc() function, it does not work for single dimensional arrays. assoc() function only works on Array of Arrays or you can say that it works on multidimensional arrays. The purpose of the assoc() function is to check the first element of each array. It processes array by checking the first element of the array with the specified index element, if the element is found then the whole array is returned otherwise the function returns nil or vacant.

Syntax:

 Array_name.assoc(Object)

Now, let us understand the implementation of assoc() function with the help of examples broadly,

Example 1:

=begin
Ruby program to demonstrate implementation of assoc() function
=end

# Initializing multiple arrays of elements 
Arr1 = ["Fruits", "banana", "apple", "orange", "kiwi", "apricot", "Pineapple"] 
Arr2 = ["Languages", "C#", "Java", "Ruby", "Python", "C++","C"] 
Arr3 = ["Colors", "Red", "Brown", "Green", "Pink", "Yellow", "Teal"]
Arr4 = ["Vegetables", "Brinjal", "Tomato", "Potato", "Reddish"] 

# Creating a final array of above arrays 
FinalArray = [Arr1, Arr2, Arr3, Arr4] 

# Invoking assoc() function 
A1 = FinalArray.assoc("Fruits") 
A2 = FinalArray.assoc("Languages") 
A3 = FinalArray.assoc("Colors")
A4 = FinalArray.assoc("Vegetables") 

# Printing the matched contained array 
puts "#{A1}"
puts "#{A2}"
puts "#{A3}"
puts "#{A4}"

Output

["Fruits", "banana", "apple", "orange", "kiwi", "apricot", "Pineapple"]
["Languages", "C#", "Java", "Ruby", "Python", "C++", "C"]
["Colors", "Red", "Brown", "Green", "Pink", "Yellow", "Teal"]
["Vegetables", "Brinjal", "Tomato", "Potato", "Reddish"]

Code logic:

In the above example, we have initialized four arrays along with their categories as the first element of the array. Finalarray is storing these arrays as its elements. When the assoc() function is invoked with the element as an index, then the element is compared with the first element of each array. Arrays A1, A2, A3, and A4 are storing the contained array.

Example 2:

=begin
Ruby program to demonstrate implementation of assoc() function
=end

# Initializing multiple arrays of elements 
Arr1 = ["Fruits", "banana", "apple", "orange", "kiwi", "apricot","Pineapple"] 
Arr2 = ["Languages", "C#", "Java", "Ruby", "Python","C++","C"] 
Arr3 = ["Colors", "Red", "Brown", "Green", "Pink","Yellow","Teal"]
Arr4 = ["Vegetables", "Brinjal", "Tomato", "Potato", "Raddish"] 

# Creating a final array of above arrays 
FinalArray = [Arr1, Arr2, Arr3, Arr4] 

# Taking input from user.
puts "Enter the catagory:-"
cat = gets.chomp
arr = FinalArray.assoc(cat)
if (arr==nil)
	puts "category not available"
else
	puts "The available elements under the category is:-"
puts "#{arr}"
end

Output

Run 1:-
Enter the category:-
Colors
The available elements under the category is:-
["Colors", "Red", "Brown", "Green", "Pink", "Yellow", "Teal"]

Run 2:-
Enter the category:-
Cars
category not available

Run 3:-
Enter the category:-
Fruits
The available elements under the category is:-
["Fruits", "banana", "apple", "orange", "kiwi", "apricot", "Pineapple"]

Code logic:

In this example, we have declared four arrays and then the final array. We are taking category as input from the user. We are passing that category to the assoc() function. If the category matches, the array is stored in the contained array otherwise assoc() is returning nil.



Comments and Discussions!

Load comments ↻





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