红宝石 |数组类 fetch() 操作
Array#fetch() : fetch()是一个 Array 类方法,它返回参数索引位置的元素。
Syntax: Array.fetch()
Parameter: index value
Return: element at the argument index value
代码 #1:fetch() 方法的示例
# Ruby code for fetch() method
# declaring array
a = [18, 22, 33, nil, 5, 6]
# declaring array
b = [1, 4, 1, 1, 88, 9]
# declaring array
c = [18, 22, nil, nil, 50, 6]
# fetch
puts "fetch : #{a.fetch(5)}\n\n"
# fetch
puts "fetch : #{b.fetch(0)}\n\n"
# fetch
puts "fetch : #{c.fetch(2)}\n\n"
输出 :
fetch : 6
fetch : 1
fetch :
代码 #2:fetch() 方法的示例
# Ruby code for fetch() method
# declaring array
a = ["abc", "nil", "dog"]
# declaring array
b = ["cow", nil, "dog"]
# declaring array
c = ["cat", nil, nil]
# fetch
puts "fetch : #{a.fetch(2)}\n\n"
# fetch
puts "fetch : #{b.fetch(0)}\n\n"
# fetch
puts "fetch : #{c.fetch(1)}\n\n"
输出 :
fetch : dog
fetch : cow
fetch :