日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

assoc_Ruby assoc()函数

發(fā)布時(shí)間:2025/3/11 编程问答 58 豆豆
生活随笔 收集整理的這篇文章主要介紹了 assoc_Ruby assoc()函数 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

assoc

Ruby中的assoc()函數(shù) (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.

到目前為止,我們已經(jīng)研究了處理一維數(shù)組的函數(shù),但是如果我們談?wù)揳ssoc()函數(shù) ,則該函數(shù)不適用于一維數(shù)組。 assoc()函數(shù)僅適用于數(shù)組數(shù)組,或者可以說(shuō)它適用于多維數(shù)組。 assoc()函數(shù)的目的是檢查每個(gè)數(shù)組的第一個(gè)元素。 它通過(guò)使用指定的索引元素檢查數(shù)組的第一個(gè)元素來(lái)處理數(shù)組,如果找到該元素,則返回整個(gè)數(shù)組,否則該函數(shù)返回nil或vacant。

Syntax:

句法:

Array_name.assoc(Object)

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

現(xiàn)在,讓我們借助示例廣泛地理解assoc()函數(shù)的實(shí)現(xiàn),

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.

在上面的示例中,我們初始化了四個(gè)數(shù)組及其類別,作為數(shù)組的第一個(gè)元素。 Finalarray將這些數(shù)組作為元素存儲(chǔ)。 當(dāng)使用該元素作為索引調(diào)用assoc()函數(shù)時(shí) ,該元素將與每個(gè)數(shù)組的第一個(gè)元素進(jìn)行比較。 數(shù)組A1,A2,A3和A4正在存儲(chǔ)包含的數(shù)組。

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}" 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 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.

在此示例中,我們聲明了四個(gè)數(shù)組,然后聲明了最后一個(gè)數(shù)組。 我們將類別作為用戶的輸入。 我們將該類別傳遞給assoc()函數(shù) 。 如果類別匹配,則將數(shù)組存儲(chǔ)在包含的數(shù)組中,否則assoc()返回nil。

翻譯自: https://www.includehelp.com/ruby/assoc-function.aspx

assoc

總結(jié)

以上是生活随笔為你收集整理的assoc_Ruby assoc()函数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。