Home »
Ruby Tutorial
Creating Array with Array.new(size, obj) in Ruby
By IncludeHelp Last updated : November 17, 2024
In the previous article, we have learnt how we can declare an Array class instance with the help of Array.[](*args) method? You can also notice that in the program codes written to demonstrate all those methods are having Array instances declared with the conventional method such as,
array_name = ['ele1', 'ele2', 'ele3', ..., 'eleN']
Now, after reading the previous article, we have learnt to declare an Array class object in the following way as well,
array_name = Array.[](*args)
The above is the way we have used it in the last article. In the upcoming articles, you will learn the different ways through which we can declare an Array instance. Well, in this article, we will see how we can declare an Array object with the help of Array.new(size, obj) method?
Method Description
This method is a public class method. This method accepts two arguments, the first one is the size of the object you want to create and the second one is the element you want to store in the Array. It will replicate the element and create the size copies of that element and store it in your Array object. You should go for this method if you want to store the same element for more than one time.
Syntax
array_name = Array.new(size = 0, obj = nil)
Parameters
Arguments play a very important role in this method. This method will take two arguments, the first one is the size and the second one is the element. If you don't provide any arguments, it will result in an empty Array with no elements in it.
Example 1
=begin
Ruby program to demonstrate Array.new(size,obj)
=end
# array declaration
arr = Array.new(size = 5, obj = "Hrithik")
# printing array elements
puts "Elements of \'arr\' are:"
puts arr
# creating an empty array
arr1 = Array.new()
puts "Number of elements present in \'arr1\' are: #{arr1.count}"
Output
Elements of 'arr' are:
Hrithik
Hrithik
Hrithik
Hrithik
Hrithik
Number of elements present in 'arr1' are: 0
Explanation
With the help of the above code, you can easily understand the implementation of the Array.new(size, obj) method. This method creates a copy of the same element for size times. You can also observe that if we are not giving parameters to the method, then the Array results into an empty Array and we have shown that with the help of Array.count method.
Example 2
=begin
Ruby program to demonstrate Array.new(size, obj)
=end
# input element
puts "Enter the element you want to keep in the Array instance"
ele1 = gets.chomp
# input array size
puts "Enter the size of Array"
siz = gets.chomp.to_i
# creating array
arr = Array.new(size = siz, obj = ele1)
# printing array elements
puts "Array elements are:"
puts arr
Output
RUN 1:
Enter the element you want to keep in the Array instance
IncludeHelp
Enter the size of Array
5
Array elements are:
IncludeHelp
IncludeHelp
IncludeHelp
IncludeHelp
IncludeHelp
RUN 2:
Enter the element you want to keep in the Array instance
100
Enter the size of Array
3
Array elements are:
100
100
100
Explanation
In the above code, you can observe that we are taking two inputs from the user first one is the size of Array and the second one is the element we want to store in it. You can see from the output that our Array instance is containing that element in size numbers.