红宝石 |数组 at() 操作
Array#at() : at()是一个 Array 类方法,它返回特定参数索引值处的元素。
Syntax: Array.at()
Parameter:
- Arrays to search elements.
- index to search
Return: Array element at a specific index value
代码 #1:at() 方法的示例
# Ruby code for at() 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]
# finding the array element at
# given index
puts "element : #{a.at(4)}\n\n"
puts "element : #{a.at(-1)}\n\n"
puts "element : #{b.at(-2)}\n\n"
puts "element : #{c.at(-4)}\n\n"
输出 :
element : 5
element : 6
element : 88
element : 33
代码 #2:at() 方法的示例
# Ruby code for at() method
# declaring array
a = ["abc", "xyz", "dog"]
# declaring array
b = ["cow", "cat", "dog"]
# declaring array
c = ["cat", "1", "dog"]
# finding the array element at
# given index
puts "element : #{a.at(4)}\n\n"
puts "element : #{a.at(-1)}\n\n"
puts "element : #{b.at(-2)}\n\n"
puts "element : #{c.at(2)}\n\n"
输出 :
element :
element : dog
element : cat
element : dog