From: Todd Benson Date: 2008-08-18T08:23:41+09:00 Subject: Re: Matrix: Need help to understand this behavior On Sun, Aug 17, 2008 at 9:53 AM, Marcio Braga wrote: > Zacek and all: how to duplicate a matrix that has elements that are also > matrix ? > For example: > > a = [[1]] > b = a.dup > puts a.object_id > puts b.object_id > puts a[0].object_id # the same object > puts b[0].object_id # the same object > b[0][0] = 2 * b[0][0] > p a > p b > > So, matrix "a" and "b" are different objects but each internal matrix > element continue to be the same object. Do I need to duplicate each one > ? It's a Matrix, right? (some of this stolen from a previous thread)... require 'matrix' class Matrix def []=(i, j, n) @rows[i][j] = n end end m = Matrix[['a', 'b'], ['c', 'd']] k = m.clone k[0, 0] = 'e' puts k.to_a puts m.to_a Todd