Cloning an array with its contents
I want to make a copy of an array so that I can change the copy in place without affecting the original one. This code doesn't work
a = [
'462664',
'669722',
'297288',
'796928',
'584497',
'357431'
]
b = a.clone
b.object_id == a.object_id # => false
a[1][2] = 'X'
a[1] #66X722
b[1] #66X722
The copy must be different from the object. Why does this work if it's just a link?
source to share
You need to make a deep copy of your array.
Here's how to do it
Marshal.load(Marshal.dump(a))
This is because you are cloning the array, but not internally. So the array object is different, but the elements it contains are the same. For example, you can do a.each{|e| b << e.dup}
for your case
source to share
You can use #dup, which creates a shallow copy of an object, which means "the object's instance variables are copied, but not the objects they refer to." For example:
a = [1, 2, 3]
b = a.dup
b # => [1, 2, 3]
Source: https://ruby-doc.org/core-2.5.3/Object.html#method-i-dup
Edit: Listen to Paul below me. I misunderstood the question.
source to share