Corruption when modifying array element

When modifying an element in a row, the result becomes corrupted when I
try to read it back. new_row is being modified correctly. It is then
being appended to new_rows (plural). But when I read new_rows back,
each element contains only the last new_row created. What gives?

$ cat test.rb;./test.rb
#!/usr/bin/env ruby

def test1
row = [ ‘field1’ , ‘field2’, ‘field3’ ]
test = [ ‘test1’, ‘test2’, ‘test3’ ]
new_rows = Array.new
new_row = row
test.each { |value|
new_row[2] = value
puts “new_row: #{new_row}”
new_rows << new_row
}
puts
for ixt in 0…new_rows.size - 1
puts “new_rows[#{ixt}]: #{new_rows[ixt]}”
end
end
test1

new_row: field1field2test1
new_row: field1field2test2
new_row: field1field2test3

new_rows[0]: field1field2test3
new_rows[1]: field1field2test3
new_rows[2]: field1field2test3

On May 30, 9:34 pm, Alan L. [email protected] wrote:

When modifying an element in a row, the result becomes corrupted
def test1
row = [ ‘field1’ , ‘field2’, ‘field3’ ]
test = [ ‘test1’, ‘test2’, ‘test3’ ]
new_rows = Array.new
new_row = row

At this point, new_row is the same object as row – it is basically a
reference to the Array object you created on the first line of test1.

test.each { |value|
new_row[2] = value
puts “new_row: #{new_row}”
new_rows << new_row

Everytime you do this, you are putting a reference to (new_)row into
your new array. All three references refer to the same Array object,
which you created on the first line. If you need to do something like
this, you can use Array’s clone method to create a copy of the array.
It will be a new object:

arr1 = [1, 2, 3]
arr2 = arr1
arr3 = arr1.clone

puts arr1.object_id
=> 24377410
puts arr2.object_id
=> 24377410
puts arr3.object_id
=> 24324410

So, try:

def test1
row = [ ‘field1’ , ‘field2’, ‘field3’ ]
test = [ ‘test1’, ‘test2’, ‘test3’ ]
new_rows = []
test.each { |value|
new_row = row.clone
new_row[2] = value
puts “new_row: #{new_row}”
new_rows << new_row
}

new_rows.each_with_index do |v, i|
puts “new_rows[#{i}]: #{new_rows[i]}”
end
end