Home » Ruby programming

Array.fill() Method with Example in Ruby (1)

Ruby Array.fill() Method | Type 1: Here, we are going to learn about the Array.fill() method with examples in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on February 03, 2020

Array.fill() Method

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.

Method description:

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(obj) -> arr

The Array instance will be populated with the object which is passed with the method.

Syntax:

    array_instance.fill(object)

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 element you want to insert"
ele = gets.chomp
array1.fill(ele)

puts "Array elements are:"
puts array1

Output

Array fill implementation.
Enter the element you want to insert
 vasu
Array elements are:
vasu
vasu
vasu
vasu
vasu
vasu
vasu
vasu

Explanation:

You can observe in the above example that when the object is passed with the method then it has overwritten all the elements present in the Array instances which we stored at the time of declaration of the Array instance.

Type 2: fill(obj, start [, length])

This method will not populate the Array instance with the same object. In this method, you will have to pass the object along with the index from where you want to insert the element and up to where you want to populate the Array instance with the same object.

Syntax:

    array_instance.fill(obj,start[,length])

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 element you want to insert:"
ele = gets.chomp

puts "From where you want to start populating:"
st = gets.chomp.to_i

puts "Up to where you want to start populating:"
pp = gets.chomp.to_i

array1.fill(ele,st,pp)

puts "Array elements are:"
puts array1

Output

Array fill implementation.
Enter the element you want to insert:
 Amisha
From where you want to start populating:
 2
Up to where you want to start populating:
 4
Array elements are:
Kumar
Ramesh
Amisha
Amisha
Amisha
Amisha
Satyam
Harish

Explanation:

In the above code, you can observe that we are asking the user for the object, form, and length. The user has entered 3 as the starting index and 3 is the number of repetitions of the object. So, you can observe that the object has been inserted at the 3rd index and has been repeated for three times by overwriting the already present elements of the object of Array class.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.