Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




RUBY - COPY FROM ARRAY


How to copy one Array to the other?


There are two ways by which we can copy one Array to the other.

  1. Using the method Array()

  2. Using the method copy()

Let us look at the first way using the Array() method.


Example :



x = ["Mohan", "Kriti", "Salim"]
y = Array(x)
puts "The Copied Array is : #{y}"


Output :



  The Copied Array is : ['Mohan', 'Kriti', 'Salim']

So, in the above code we have created a Array and initialised to the variable x.


x = ["Mohan", "Kriti", "Salim"]

Below is how the values are positioned in the Array,

java_Collections

Then we have used the Array() method to take the Array x as parameter and create a new Array that would be the exact copy of x.


Then assign it to y.


	y = Array(x)
java_Collections


And we get the below output,


The Copied Array is : ['Mohan', 'Kriti', 'Salim']

Now, let us look at the second way of copying a Array using =.


Example :



x = ["Mohan", "Kriti", "Salim"]
y = x
puts "The Copied Array is : #{y}"


Output :



  The Copied Array is ['Mohan', 'Kriti', 'Salim']

So, in the above code we have created a Array and initialised to the variable x.


x = ["Mohan", "Kriti", "Salim"]

Below is how the values are positioned in the Array,

java_Collections

Then we have used the assignment operator, = that would create an exact copy of x. And assign it to y.


	y = x
java_Collections


And we get the below output,


The Copied Array is  ['Mohan', 'Kriti', 'Salim']