From: J J Date: 2001-05-20T10:11:25+09:00 Subject: [ruby-talk:15410] Re: How to clone an object? How about: class A attr_reader :a attr_writer :a def initialize @a = [1, 2] end end anA = A.new puts anA.a anotherA = Marshal.load(Marshal.dump(anA)) puts anotherA.a anA.a = [4,5] puts "anA.a = #{anA.a}" puts "anotherA.a = #{anotherA.a}" Regards, JJ -- Be Kind, Be Careful, Be Yourself -- Solid, reliable software at reasonable prices http://www.johnjohnsonsoftware.com "Jim Freeze" wrote in message news:Pine.BSF.4.32.0105171456230.70031-100000@www.stelesys.com... > On Fri, 18 May 2001, Dave Thomas wrote: > > 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] > > > > Thanks, that works well. But, what if a is private. > > class K > def initialize > @a = [1,2] > end > > def clone > copy = super > copy.a = @a.dup > copy > end > end > > k1 = K.new > k2 = k1.clone #=> NameError: undefined method `a=' for # > > Thanks > > > ========================================================= > Jim Freeze > jim@freeze.org > --------------------------------------------------------- > No comment at this time. > http://www.freeze.org > ========================================================= > >