From: Dave Thomas Date: 2001-05-18T03:51:13+09:00 Subject: [ruby-talk:15338] Re: How to clone an object? Jim Freeze writes: > Sorry for responding to my own post, but the > text below may be a bit more clear. > > I have run across the following interesting situtation: > > class K > attr_accessor :a > def initialize > @a = [1,2] > end > end > The question is, how do I get a copy of > k2 where k2.a is a copy of k1.a and not a reference? You could do something like: class K attr_accessor :a def initialize @a = [1,2] end def clone copy = super copy.a = a.clone copy end end k1 = K.new k2 = k1.clone k1.a[0] = 99 k1.a #=> [99, 2] k2.a #=> [1, 2] Regards Dave