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
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.
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: