Ruby中的assoc()函数用于搜索其第一个元素与该函数的索引进行比较的数组数组,如果找到匹配项,则返回包含的数组,否则返回nil或vacant。
用法: Array.assoc(Object)
Here Array is the array of arrays.
参数:
Object :它是一个与包含的数组的第一个元素进行比较的元素。
返回值:如果找到匹配项,则包含的数组否则将返回nil或vacant。
示例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}"
输出:
["Alphabets", "a", "b", "c", "d", "e"] ["City", "Kolkata", "Mumbai", "Delhi", "Patna"] ["Names", "gfg", "Geeks", "Geek", "GeeksforGeeks"]
示例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}"
输出:
["Alphabets", "a", "b", "c", "d", "e"] ["Names"]
参考: https://devdocs.io/ruby~2.5/array#method-i-assoc
相关用法
- Ruby Hash assoc()用法及代码示例
- Ruby Array min()用法及代码示例
- Ruby Array none?()用法及代码示例
- Ruby Array max()用法及代码示例
- Ruby Array map()用法及代码示例
- Ruby Array zip()用法及代码示例
- Ruby Array one?()用法及代码示例
- Scala Array &()用法及代码示例
- Ruby Array take()用法及代码示例
- Ruby Array pop()用法及代码示例
- Ruby Array class last()用法及代码示例
- Ruby Array to_s()用法及代码示例
- Ruby Array transpose()用法及代码示例
注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 Ruby | Array assoc() function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。