Ruby - Array Array Cloning

Introduction

When you use the assignment operator (=) to assign one array variable to another variable, you are assigning a reference to the array.

If you assign one array called arr1 to another array called arr2, any changes made to either variable will also alter the value of the other because both variables refer to the same array.

If you want the variables to reference two different arrays, you can use the clone method to make a new copy:

Demo

arr1=['h','e','l','l','o',' ','w','o','r','l','d'] 
arr2=arr1  # arr2 is now the same as arr1.  
           # Change arr1 and arr2 changes too! 
arr3=arr1.clone # from  ww w . ja  va2 s  .co  m
           # arr3 is a copy of arr1.  
           # Change arr3 and arr2 is unaffected