From: Sebastian Hungerecker Date: 2007-12-25T21:49:44+09:00 Subject: Re: Problem with copying array Matthew Harris wrote: > Bryan already mentioned this, but for primitives like strings, integers, > floats and whatnot, Ruby knows how to copy these objects Strings can be copied with the dup method, yes. But if you mean to imply that that happens automatically when dupping an array of Strings, you are wrong: >> arr=["chunky", "bacon"] >> arr.dup.each {|str| str.tr!("ck","kc")} >> arr => ["khuncy", "bakon"] Integers and Floats can not be copied at all: >> 5.dup TypeError: can't dup Fixnum >> 5.0.dup TypeError: allocator undefined for Float > but if you > have custom classes or other classes that don't define their own > behavior for copying, then Ruby will keep the reference. If you dup an array you will always get a new array with references to the old objects. dup never makes a deep copy. Presumably there is no built-in way to make a deep copy because, as mentioned above, some objects (like Integers) can't be copied. I hope this cleared the confusion a bit, Sebastian -- Jabber: sepp2k@jabber.org ICQ: 205544826