From: Csaba Henk Date: 2005-01-26T23:30:48+09:00 Subject: Re: clone and dup On 2005-01-26, Robert Klemme wrote: > > "Keith P. Boruff" schrieb im Newsbeitrag > news:89KJd.989$HR6.889@fe10.lga... >> After reading about these two methods for the Object class in "Pragmatic >> Programmer", I'm still having a hard time understanding the differences. >> Can someone point me to some better info on these or explain it (I do >> understand the differences between deep and shallow copying). > > They do both shallow copy. > > Here's a difference: > >>> s="foo".freeze >=> "foo" >>> s.frozen? >=> true >>> s.clone.frozen? >=> true >>> s.dup.frozen? >=> false Or: irb(main):189:0> o=Object.new => # irb(main):190:0> def o.foo; end => nil irb(main):191:0> o2=o.clone => # irb(main):192:0> o2.foo => nil irb(main):193:0> o3=o.dup => # irb(main):194:0> o3.foo NameError: undefined method `foo' for # from (irb):194 from /usr/lib/ruby/1.8/yaml/rubytypes.rb:67 Csaba