Open In App

Ruby | Array assoc() function

Last Updated : 09 Jan, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The assoc() function in Ruby is used to search through an array of arrays whose first element is compared with the index of the function and return the contained array if match found otherwise return either nil or vacant.

Syntax: Array.assoc(Object)
Here Array is the array of arrays.

Parameters:
Object : It is an element which gets compared with the first element of the contained array.

Returns: the contained array if match found otherwise returns either nil or vacant.

Example 1:




# Initializing a array of elements
Array1 = ["Alphabets", "a", "b", "c", "d", "e"]
Array2 = ["Names", "gfg", "Geeks", "Geek", "GeeksforGeeks"]
Array3 = ["City", "Kolkata", "Mumbai", "Delhi", "Patna"]
  
# Creating an array of above arrays
Array = [Array1, Array2, Array3]
  
# Calling assoc() function
A = Array.assoc("Alphabets")
B = Array.assoc("City")
C = Array.assoc("Names")
  
# Printing the matched contained array
puts "#{A}"
puts "#{B}"
puts "#{C}"


Output:

["Alphabets", "a", "b", "c", "d", "e"]
["City", "Kolkata", "Mumbai", "Delhi", "Patna"]
["Names", "gfg", "Geeks", "Geek", "GeeksforGeeks"]

Example 2:




# Initializing a array of elements
Array1 = ["Alphabets", "a", "b", "c", "d", "e"]
Array2 = ["Names"]
Array3 = "City"
  
# Creating an array of above arrays
Array = [Array1, Array2, Array3]
  
# Calling assoc() function
A = Array.assoc("Alphabets")
B = Array.assoc("City")
C = Array.assoc("Names")
  
# Printing the matched contained array
puts "#{A}"
puts "#{B}"
puts "#{C}"


Output:

["Alphabets", "a", "b", "c", "d", "e"]

["Names"]

Reference: https://devdocs.io/ruby~2.5/array#method-i-assoc



Similar Reads

Ruby | Hash assoc() function
assoc() is an Hash class method which searches an element through the Hash. Syntax: Hash.assoc() Parameter: Hashs for finding elements. Return: searches an element through the Hash Example #1: # Ruby code for assoc() method # declaring Hash value a = { "a" => 100, "b" => 200 } # declaring Hash value c = {"a" =
1 min read
Difference between Ruby and Ruby on Rails
1. Ruby : Ruby is an object-oriented scripting language launched in 1995 and is known as a general-purpose programming language. It was programmed in C programming language. Ruby is a secured programming language and its syntax is similar to Perl and Python. It was developed on the principle of user interface design and it is mainly used to develop
3 min read
Ruby | Array Concatenation using (+) function
Array#+() is a Array class method which performs set concatenate operation arrays by combining two arrays to a third array. Syntax: Array.+() Parameter: Arrays for performing the concatenation operation. Return: New arrays by combining two arrays. Example #1 : # Ruby code for +() method # showing concatenate operation # declaring array a = [18, 22,
2 min read
Ruby | Array abbrev() function
abbrev() is an Array class method which provides unambiguous set of abbreviations for the string. Syntax: Array.abbrev()Parameter:- string for abbreviation - pattern [optional]Return: unambiguous abbreviations set Example #1 : Example for abbrev() method C/C++ Code # Ruby code for abbrev() method # checking for unambiguous abbreviations require 'ab
1 min read
Ruby Array () function with example
Array#() : () is an Array class method which performs the comparison between the two arrays. Syntax: Array.() Parameter: Array for the comparison Return: -1 : array is less than the other array 0 : array is equal to other array 1 : array is greater than the other array Example #1 : # Ruby code for <=>() method # showing comparison # declaring
2 min read
Ruby | Array class first() function
first() is a Array class method which returns the first element of the array or the first 'n' elements from the array. Syntax: Array.first() Parameter: Array n - no. of elements Return: first element of the array or the first 'n' elements from the array Example #1 : # Ruby code for first() method # declaring array a = [18, 22, 33, nil, 5, 6] # decl
1 min read
Ruby | Array class flatten() function
flatten() is an Array class method which returns flattened array i.e. a 1D array Syntax: Array.flatten() Parameter: Array Return: 1D array Example #1 : # Ruby code for flatten() method # declaring array a = [[18, 22], [ 33, nil, 5, 6]] # declaring array b = [[[1, 4, 1, 1, 88, 9]]] # flatten puts "flatten : #{a.flatten()}\n\n" # flatten pu
1 min read
Ruby | Array class flatten!() function
flatten!() is a Array class method which returns the flattened the array and returns nil if there is no modification required Syntax: Array.flatten!() Parameter: Array Return: flattened array or nil if no modification is required Example #1 : # Ruby code for flatten!() method # declaring array a = [[18, 22], [ 33, nil, 5, 6]] # declaring array b =
1 min read
Ruby | Array class inspect() function
Inspect() is an Array class method which returns the all array elements. Syntax: Array.inspect() Parameter: Array Return: all array elements Example #1 : # Ruby code for inspect() method # declaring array a = [18, 22, 33, nil, 5, 6] # declaring array b = [1, 4, 1, 1, 88, 9] # declaring array c = [18, 22, nil, nil, 50, 6] # inspect puts "inspec
1 min read
Ruby | Array class hash() function
hash() is an Array class method which returns the hash code of the array elements Syntax: Array.hash() Parameter: Array Return: hash code of the array elements Example #1 : # Ruby code for hash() method # declaring array a = [18, 22, 33, nil, 5, 6] # declaring array b = [1, 4, 1, 1, 88, 9] # declaring array c = [18, 22, nil, nil, 50, 6] # hash puts
1 min read