I have a class that contains an array. This is an example class. a
is an array
class Holder
attr_accessor :a
end
I am trying to make a copy of an object and execute a function on its array. An example situation:
t = Holder.new
t.a = (1..9).to_a
t2= Holder.new
t2.a = t.a
t2.a[2]+=10
t2.a
# => [1, 2, 13, 4, 5, 6, 7, 8, 9]
t.a
# => [1, 2, 13, 4, 5, 6, 7, 8, 9]
Both array in each object are effected. I don't know how to make them separate. I tried with clone
and dup
too.
dupt = t2.dup
dupt.a[8]+=10
dupt
# => #<Holder:0x007fb6e193b0a8 @a=[1, 2, 13, 4, 5, 6, 7, 8, 19]>
t2
# => #<Holder:0x007fb6e1962ba8 @a=[1, 2, 13, 4, 5, 6, 7, 8, 19]>