pwst
pwst

Reputation: 1

Array of arrays and shared memory

I am creating a nested array. When I build a new row and append it to the nested array, clear the row using the method clear, and start over building another new row, it clears the row in both arrays. Any idea why?

code snippet:

new_rec = Array.new
new_rec << "string1"
new_rec << "string2"
new_rec << "string3"
new_rec << "string4"

new_csv = Array.new
new_csv << new_rec
new_rec.clear
new_csv #=> [[]]

Upvotes: 0

Views: 68

Answers (1)

sawa
sawa

Reputation: 168239

It is because you have new_rec in new_csv, and you cleared the object new_rec.

Upvotes: 1

Related Questions