# Corruption when modifying array element

**URL:** <https://www.ruby-forum.com/t/corruption-when-modifying-array-element/105561>\
**Category:** Ruby\
**Created:** [May 31, 2007, 4:34am UTC](https://www.ruby-forum.com/t/corruption-when-modifying-array-element/105561 "2007-05-31T04:34:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![alake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f04885/32.png) [@alake](https://www.ruby-forum.com/u/alake)\
**Post date:** [May 31, 2007, 4:34am UTC](https://www.ruby-forum.com/t/corruption-when-modifying-array-element/105561/1 "2007-05-31T04:34:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![alake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f04885/32.png) [@alake](https://www.ruby-forum.com/u/alake)\
**Post date:** [May 31, 2007, 5:36am UTC](https://www.ruby-forum.com/t/corruption-when-modifying-array-element/105561/2 "2007-05-31T05:36:11Z")

</div>

On May 30, 9:34 pm, Alan L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
