copy an array in Ruby
Code snippet on how to copy an array in Ruby
array1 = [1, 2, 3]
array2 = array1.dup
This code snippet creates two separate arrays, array1 and array2, that both contain the same elements. The first line declares and initialises array1 as an array with three elements - 1, 2, and 3. The second line uses the dup
method to copy the elements in array1 into array2. dup
creates a shallow copy of the array - meaning it copies the elements in array1 but into a separate array with the same values. So, both arrays, array1 and array2, contain the same elements, [1, 2, 3].