Ruby 数组rassoc()操作
Array#rassoc() : rassoc() 是一个数组类方法,在数组中搜索一个元素。
语法: Array.rassoc()
参数:
– 用于寻找元素的数组。
– 要检测的元素
返回:在最后添加元素后的数组。
代码#1:rassoc()方法的例子
# Ruby code for rassoc() method
# declaring array
a = [18, 22, 33, 4, 5, 6]
# declaring array
b = [5, 4, 22, 1, 88, 9]
# declaring array
c = [18, 22, 33, 40, 50, 16]
ar = [a, b, c]
# searching for an element
puts "check : #{ar.rassoc(4)}\n\n"
puts "check : #{ar.rassoc(22)}\n\n"
输出:
check : [5, 4, 22, 1, 88, 9]
check : [18, 22, 33, 4, 5, 6]
代码#2:rassoc()方法的例子
# Ruby code for rassoc() method
# declaring array
a = ["abc", "xyz", "dog"]
# declaring array
b = ["cow", "cat", "dog"]
# declaring array
c = ["cat", "1", "dog"]
ar = [a, b, c]
# searching for an element
puts "check : #{ar.rassoc("xyz")}\n\n"
puts "check : #{ar.rassoc("1")}\n\n"
输出:
check : ["abc", "xyz", "dog"]
check : ["cat", "1", "dog"]