Ruby | Array class fill() operation
Array#fill() : fill() is a Array class method which fills the array with the element and that can be in a specific range.
Syntax: Array.fill()
Parameter: Array, element, range
Return: array with specific element in the given range.
Code #1 : Example for fill() method
a = [ 18 , 22 , 33 , nil , 5 , 6 ]
b = [ 1 , 4 , 1 , 1 , 88 , 9 ]
c = [ 18 , 22 , nil , nil , 50 , 6 ]
puts "fill : #{a.fill(5)}\n\n"
puts "fill : #{b.fill(0, 12)}\n\n"
puts "fill : #{c.fill(20, 0, 1)}\n\n"
|
Output :
fill : [5, 5, 5, 5, 5, 5]
fill : [1, 4, 1, 1, 88, 9]
fill : [20, 22, nil, nil, 50, 6]
Code #2 : Example for fill() method
a = [ "abc" , "nil" , "dog" ]
b = [ "cow" , nil , "dog" ]
c = [ "cat" , nil , nil ]
puts "fill : #{a.fill(5)}\n\n"
puts "fill : #{b.fill(0, 12)}\n\n"
puts "fill : #{c.fill(20, 0, 1)}\n\n"
|
Output :
fill : [5, 5, 5]
fill : ["cow", nil, "dog"]
fill : [20, nil, nil]
Last Updated :
08 Jan, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...