From: Kent Sibilev Date: 2004-03-14T00:21:10+09:00 Subject: Re: Object#clone and Object#dup Object#dup or Object#clone methods create a shallow copy of the object. If you need a deep copy you have to do it either like this: $ irb irb(main):001:0> class Klass irb(main):002:1> attr_accessor :str irb(main):003:1> def initialize_copy(o) irb(main):004:2> @str = o.str.dup irb(main):005:2> end irb(main):006:1> end => nil irb(main):007:0> s1 = Klass.new => # irb(main):008:0> s1.str = "Hello" => "Hello" irb(main):009:0> s2 = s1.dup => # irb(main):010:0> s2.str[1,4] = 'i' => "i" irb(main):011:0> s1.str => "Hello" irb(main):012:0> s2.str => "Hi" irb(main):013:0> or like that: $ irb irb(main):001:0> class Klass irb(main):002:1> attr_accessor :str irb(main):003:1> end => nil irb(main):004:0> s1 = Klass.new => # irb(main):005:0> s1.str = "Hello" => "Hello" irb(main):006:0> s2 = Marshal.load(Marshal.dump(s1)) => # irb(main):007:0> s2.str[1,4] = 'i' => "i" irb(main):008:0> s1 => # irb(main):009:0> s1.str => "Hello" irb(main):010:0> s2.str => "Hi" irb(main):011:0> Cheers, Kent. On Mar 13, 2004, at 8:49 AM, Raphael Bauduin wrote: > Hi, > > I'd like to duplicate n object, work on the copy, and be sure the > original > isn't change at all. How can I do that? > > From the pickaxe (example similar to what I'm working on): > > class Klass > attr_accessor :str > end > > s1 = Klass.new > => # > s1.str = "Hello" > => "Hello" > s2 = s1.dup > => # > s2.str[1,4] = "i" > => "i" > > s1.str > => "Hi" > > s2.str > => "Hi" > > I would like that s1.str still returns "Hello". I tried with dup and > clone, > with the same result.... What method should I use? > > Thanks > > Raph > >