红宝石 |数组 values_at()函数
Array#values_at() : values_at()是一个 Array 类方法,它返回一个数组,其中包含与给定选择器相对应的 self 中的元素。
Syntax: Array.values_at()
Parameter: Array
Return: an array containing the elements in self corresponding to the given selector
示例 #1:
# Ruby code for values_at() method
# declaring array
a = [18, 22, 33, nil, 5, 6]
# declaring array
b = [1, 4, 1, 1, 88, 9]
# declaring array
c = [18, 22, 50, 6]
# values_at method example
puts "values_at() method form : #{a.values_at(2, 4)}\n\n"
puts "values_at() method form : #{b.values_at(1, 5)}\n\n"
puts "values_at() method form : #{c.values_at(0, 3)}\n\n"
输出 :
values_at() method form : [33, 5]
values_at() method form : [4, 9]
values_at() method form : [18, 6]
示例 #2:
# Ruby code for values_at() method
# declaring array
a = ["abc", "nil", "dog"]
# declaring array
c = ["cat", nil]
# declaring array
b = ["cow", nil, "dog"]
# values_at method example
puts "values_at() method form : #{a.values_at(0)}\n\n"
puts "values_at() method form : #{b.values_at(2)}\n\n"
puts "values_at() method form : #{c.values_at(1)}\n\n"
输出 :
values_at() method form : ["abc"]
values_at() method form : ["dog"]
values_at() method form : [nil]