Home »
Ruby Tutorial
Creating Array with Array.new(Array_object) in Ruby
By IncludeHelp Last updated : November 17, 2024
In the previous article, we have learned how we can declare an Array class instance with the help of Array.new(size, obj) 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 articles, we have learnt to declare an Array class object in the following ways as well,
array_name = Array.[](*args)
array_name = Array.new(size = 0, obj = nil)
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 the Array.new(Array_object) method.
Method Description
This method is a public class method. This method is used when you want to create an Array instance with the help of an existing Array object. If you are creating a new Array with the help of the previous Array, this does not mean that the reference of both the Arrays will be the same. They will be different.
Syntax
array_name = Array.new(Array_object)
Parameters
Arguments play a very important role in this method. This method will take a previously defined Array as an argument. If you don’t provide any argument, it will result in an empty Array with no elements in it.
Example 1
=begin
Ruby program to demonstrate Array.new(Array)
=end
# array declaration
names = ['Sanjay','Ram','Shyam','Harish']
# creating array from array object
arr = Array.new(names)
# 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:
Sanjay
Ram
Shyam
Harish
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(Array) method. First, we have defined an Array instance named as ‘Name’ and we are passing that instance inside the new Array instance. 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(Array)
=end
# input array elements
puts "Enter the first element you want to keep in the Array instance"
ele1 = gets.chomp
puts "Enter the second element you want to keep in the Array instance"
ele2 = gets.chomp
puts "Enter the third element you want to keep in the Array instance"
ele3 = gets.chomp
puts "Enter the fourth element you want to keep in the Array instance"
ele4 = gets.chomp
# creating array with user input elements
arr = [ele1,ele2,ele3,ele4]
# creating array from array objects
arr_new = Array.new(arr)
puts "Array elements are:"
puts arr
Output
Enter the first element you want to keep in the Array instance
Hrithik
Enter the second element you want to keep in the Array instance
B.Tech
Enter the third element you want to keep in the Array instance
Graphics Era University
Enter the fourth element you want to keep in the Array instance
India
Array elements are:
Hrithik
B.Tech
Graphics Era University
India
Explanation
In the above code, you can observe that we are taking multiple inputs from the user and storing them inside an Array and then we are passing that Array into new Array.
Example 3
=begin
Ruby program to demonstrate the
reference of two arrays
=end
# array declaration
names = ['Sanjay','Ram','Shyam','Harish']
# creating array from array object
arr = Array.new(names)
# checking whether 'names' and 'arr' are equal or not
puts names.equal?arr
Output
false
Explanation
In the above code, you can observe that when we are equating the references of both the Arrays, we are getting the result as "false" which simply means that the reference of both the Arrays are different even if the second Array instance is created with the help of first Array instance.