From: dblack@... Date: 2007-02-17T05:05:19+09:00 Subject: Re: Oppinions on RCR for dup on immutable classes Hi -- On Sat, 17 Feb 2007, ara.t.howard@noaa.gov wrote: > On Sat, 17 Feb 2007 dblack@wobblini.net wrote: > >> You could conceivably have a case where an object can't be dup'd, but is >> mutable, like a singleton class. If dup returns self, then you could end >> up >> changing the object when you didn't want to. It's probably not an everyday >> problem... but I still don't like the idea of having to remember that "dup" >> means "dup or self". > > > you already do though? > > > > harp:~ > cat a.rb > # > # crappy > # > h = { :point => 'we already have' } > > h2 = h.dup > > h2[:point] << ' that problem' > > p h[:point] > # > # crappy > # > h = { :point => 'we already have' } > > h2 = h.clone > > h2[:point] << ' that problem' > > p h[:point] > # > # sledge hammer > # > h = { :point => 'an imperfect solution' } > > mcp = lambda{|obj| Marshal.load(Marshal.dump(obj))} > > h2 = mcp[ h ] > > h2[:point] << ' is to use marshal' > > p h[:point] > > > harp:~ > ruby a.rb > "we already have that problem" > "we already have that problem" > "an imperfect solution" I don't consider returning a shallow copy the same as returning self. It's not the same object as the receiver of the "dup" message. David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com)