Home »
Ruby Tutorial
Ruby Array.fill(start [, length] ) { |index| block }) Method
By IncludeHelp Last updated : December 01, 2024
In this article, we will study about Array.fill() method. You all must be thinking the method must be doing something related to populate the Array instance. Well, we will figure this out in the rest of our content.
Description and Usage
This method is one of the examples of the Public instance method which is specially defined in the Ruby library for Array class. This method is used to populate the Array instances. You can fill multiple objects in the object of the Array class with the help of this method. This method is one of the examples of Destructive methods. This method has many forms and we will be studying them in the rest of the content. There are two of its types are present in this article and are demonstrated with the help of syntaxes and program codes.
Type 1: fill(start [, length] ) { |index| block } -> arr
The Array instance will be populated with the indexes and you can manipulate the index as per your requirements.
Syntax
array_instance.fill(start [, length] ) { |index| block }
Example 1
=begin
Ruby program to demonstrate fill method
=end
# array declaration
Array1 = ["Kumar","Ramesh","Apple","Pappu","Sana","Yogita","Satyam","Harish"]
puts "Array fill implementation."
puts "Enter the start index from where you want to start populating:"
st = gets.chomp.to_i
puts "Enter the number of times"
pp = gets.chomp.to_i
Array1.fill(st,pp){|i| i*i}
puts "Array elements are:"
puts Array1
Output
Array fill implementation.
Enter the start index from where you want to start populating:
2
Enter the number of times
2
Array elements are:
Kumar
Ramesh
4
9
Sana
Yogita
Satyam
Harish
Explanation
You can observe in the above example that we are asking the user about two inputs first one is the starting index and another one is the number of times. You can observe that the element on the 2nd index is replaced by its index with some manipulation and though the user has entered two times then the element on the 3rd index is replaced by the manipulation of its index.
Type 2: fill(range){|index|block }
The Array instance will be populated with the indexes and you can manipulate the index as per your requirements. You have to provide the range from where and up to where you want to populate the Array instance with indexes.
Syntax:
array_instance.fill(range){|index| block}
Example 2
=begin
Ruby program to demonstrate fill method
=end
# array declaration
array1 = ["Kumar","Ramesh","Apple","Pappu","Sana","Yogita","Satyam","Harish"]
puts "Array fill implementation."
puts "Enter the starting index:"
st = gets.chomp.to_i
puts "Enter the ending index:"
pp = gets.chomp.to_i
array1.fill(st..pp){|i| i*i}
puts "Array elements are:"
puts array1
Output
Array fill implementation.
Enter the starting index:
2
Enter the ending index:
4
Array elements are:
Kumar
Ramesh
4
9
16
Yogita
Satyam
Harish
Explanation
In the above code, you can observe that we are asking the user for the first index and last index. The user has entered 2 as the starting index and 4 is the ending index. So, you can observe that on the 2nd, 3rd and 4th index, the elements are overwritten with the index manipulation.