Ruby | Array assoc() function Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like 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: Ruby # 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: Ruby # 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 Comment More infoAdvertise with us Next Article Ruby | Array to_ary() function K Kanchan_Ray Follow Improve Article Tags : Ruby Ruby-Methods Ruby Array-class Similar Reads Ruby | Array class to_s() function to_s() is an Array class method which returns the string representation of the array elements. Syntax: Array.to_s() Parameter: Array Return: string representation of the array elements. Example #1: Ruby # Ruby code for to_s() method # declaring array a = [18, 22, 33, nil, 5, 6] # declaring array b = 1 min read Ruby | Array class join() function join() is an Array class method which returns the string which is created by converting each element of the array to a string, separated by the given separator. Syntax: Array.join()Parameter: Array separatorReturn: join value of the array elements Example #1: Ruby # Ruby code for join() method # d 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 # Ruby code for hash() method # declaring array a = [18, 22, 33, nil, 5, 6] # declaring array b = [1, 4, 1, 1, 88, 9] # de 1 min read Ruby | Array to_ary() function Array#to_ary() : to_ary() is a Array class method which returns self array representation. Syntax: Array.to_ary() Parameter: Array Return: self array representation. Example #1 : Ruby # Ruby code for to_ary() method # declaring array a = [18, 22, 33, nil, 5, 6] # declaring array b = [1, 4, 1, 1, 88, 1 min read Ruby | Array to_ary() function Array#to_ary() : to_ary() is a Array class method which returns self array representation. Syntax: Array.to_ary() Parameter: Array Return: self array representation. Example #1 : Ruby # Ruby code for to_ary() method # declaring array a = [18, 22, 33, nil, 5, 6] # declaring array b = [1, 4, 1, 1, 88, 1 min read Ruby | Array class index() function index() is an Array class method which returns the index of the first object in the array. Syntax: Array.index() Parameter: Array obj - object to search for Return: Index value of the first array Example #1 : Ruby # Ruby code for index() method # declaring array a = [18, 22, 33, nil, 5, 6] # declari 1 min read Like