Home » Ruby programming

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

Ruby Array.fill() Method | Type 2: 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, range ) -> arr

The Array instance will be populated with the object which is passed with the method and the range passed with the method will be deciding from were and up to where the object is going to be stored.

Syntax:

    array_instance.fill(object,range)

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

puts "Enter the start index from where you want to start populating:"
st = gets.chomp.to_i

puts "Enter the last index up to where you want to end 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:
 Danish
Enter the start index from where you want to start populating:
 3
Enter the last index up to where you want to end populating:
 5
Array elements are:
Kumar
Ramesh
Apple
Danish
Danish
Danish
Satyam
Harish

Explanation:

In the above code, you can observe that we have asked the range and object from the user. When the user has entered 3 as the starting index and 5 as the ending index then the Array has been populated with that object at 3rd, 4th and 5th index of the Array instance.

Type 2: fill { |index| block } -> arr

This method will not populate the Array instance with the same object. In this method, you will have to pass a block in which you want to process the Array indexes. You can perform any operation of your wish inside the method block.

Syntax:

    array_instance.fill { |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."
array1.fill{|i| i*i}

puts "Array elements are:"
puts array1

Output

Array fill implementation.
Array elements are:
0
1
4
9
16
25
36
49

Explanation:

In the above code, you can observe that we have made use of indexes to populate the Array instance. You can see that the Array element is containing the cube of indexes.







Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.