From: Logan Capaldo Date: 2006-01-30T22:51:57+09:00 Subject: Re: misunderstanding the Passing of references On Jan 30, 2006, at 1:50 AM, Todd S. wrote: > Just to provide some direct information for anyone who might find this > and need the help... > > The below does indeed solve the problem... > > class Model > def deep_clone > Marshal::load(Marshal.dump(self)) > end > end > > a = Model.new > b = Model.deep_clone > > Thank you very much for your help. > > -- > Posted via http://www.ruby-forum.com/. > This isn't perfect but: class Object def deeper_copy copy = self.dup self.instance_variables.each do |ivar| copy.instance_variable_set(ivar, self.instance_variable_get(ivar).dup) end copy end end Note that this is specifically designed to work for regular style classes (ie ones that store everything in instance variables) and for Strings, Arrays and Structs its no better than (but no worse than) #dup. Hence "deeper_copy" instead of "deep_copy"