红宝石 |数组类fill()操作
Array#fill() : fill()是一个 Array 类方法,它用元素填充数组,并且可以在特定范围内。
Syntax: Array.fill()
Parameter: Array, element, range
Return: array with specific element in the given range.
代码 #1:fill() 方法的示例
# Ruby code for fill() 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]
# fill
puts "fill : #{a.fill(5)}\n\n"
# fill
puts "fill : #{b.fill(0, 12)}\n\n"
# fill
puts "fill : #{c.fill(20, 0, 1)}\n\n"
输出 :
fill : [5, 5, 5, 5, 5, 5]
fill : [1, 4, 1, 1, 88, 9]
fill : [20, 22, nil, nil, 50, 6]
代码 #2:fill() 方法的示例
# Ruby code for fill() method
# declaring array
a = ["abc", "nil", "dog"]
# declaring array
b = ["cow", nil, "dog"]
# declaring array
c = ["cat", nil, nil]
# fill
puts "fill : #{a.fill(5)}\n\n"
# fill
puts "fill : #{b.fill(0, 12)}\n\n"
# fill
puts "fill : #{c.fill(20, 0, 1)}\n\n"
输出 :
fill : [5, 5, 5]
fill : ["cow", nil, "dog"]
fill : [20, nil, nil]