From: James Gray Date: 2008-12-29T02:25:35+09:00 Subject: Re: New to Ruby: copying arrays? On Dec 28, 2008, at 5:02 AM, Stefano Crocco wrote: > Alle Sunday 28 December 2008, John Park ha scritto: >> Oh, one more question: what's the difference between .dup and .clone >> methods? > > As far as I know, the only difference is that clone also copies the > frozen > state of the original object, while dup doesn't: > s1 = "test" > s1.freeze > s2 = s1.dup > s3 = s1.clone > puts s2.frozen? #=> false > puts s3.frozen? #=> true clone() also copies an object's singleton class: >> s = "cat" => "cat" >> def s.speak; "purr" end => nil >> s.speak => "purr" >> s.dup.speak NoMethodError: undefined method `speak' for "cat":String from (irb):4 >> s.clone.speak => "purr" I learned this and other great things from these screencasts: http://www.pragprog.com/screencasts/v-dtrubyom/the-ruby-object-model-and-metaprogramming James Edward Gray II