ruby - Cloning an array with its content -


i want make copy of array, modify copy in-place, without affecting original one. code fails

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 should different object. why act if reference?

you need deep copy of array.

here way

marshal.load(marshal.dump(a)) 

this because cloning array not elements inside. array object different elements contains same instances. could, example, a.each{|e| b << e.dup} case


Comments