assoc_Ruby assoc()函数
assoc
Ruby中的assoc()函數 (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.
到目前為止,我們已經研究了處理一維數組的函數,但是如果我們談論assoc()函數 ,則該函數不適用于一維數組。 assoc()函數僅適用于數組數組,或者可以說它適用于多維數組。 assoc()函數的目的是檢查每個數組的第一個元素。 它通過使用指定的索引元素檢查數組的第一個元素來處理數組,如果找到該元素,則返回整個數組,否則該函數返回nil或vacant。
Syntax:
句法:
Array_name.assoc(Object)Now, let us understand the implementation of assoc() function with the help of examples broadly,
現在,讓我們借助示例廣泛地理解assoc()函數的實現,
Example 1:
范例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.
在上面的示例中,我們初始化了四個數組及其類別,作為數組的第一個元素。 Finalarray將這些數組作為元素存儲。 當使用該元素作為索引調用assoc()函數時 ,該元素將與每個數組的第一個元素進行比較。 數組A1,A2,A3和A4正在存儲包含的數組。
Example 2:
范例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" elseputs "The available elements under the category is:-" puts "#{arr}" endOutput
輸出量
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 availableRun 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.
在此示例中,我們聲明了四個數組,然后聲明了最后一個數組。 我們將類別作為用戶的輸入。 我們將該類別傳遞給assoc()函數 。 如果類別匹配,則將數組存儲在包含的數組中,否則assoc()返回nil。
翻譯自: https://www.includehelp.com/ruby/assoc-function.aspx
assoc
總結
以上是生活随笔為你收集整理的assoc_Ruby assoc()函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HTML中的类属性
- 下一篇: java 方法 示例_Java语言环境g