From: "(r.*n){2}" Date: 2010-09-28T05:15:10+09:00 Subject: Re: copying a matrix. On Sep 27, 3:47 pm, John Smith wrote: > How can I create an array copy that > does not reference the same object... > > I have tried this without luck. > > m2 = m.map { |e| e } > m2[2][2]=2 > > p m > p m2 > -- > Posted viahttp://www.ruby-forum.com/. m2 = m.dup 0.upto(m2.size - 1) { |i| m2[i] = m[i].dup; 0.upto(m2[i].size - 1) { | j| m2[i][j] = m[i][j] } } m2[2][2] = 100 p m p m2 [[1, 2, 3], [4, 5, 6], [7, 8, 9]] [[1, 2, 3], [4, 5, 6], [7, 8, 100]]