From: Frederick Cheung Date: 2007-12-25T01:16:15+09:00 Subject: Re: Problem with copying array On 24 Dec 2007, at 16:05, jbieger@gmail.com wrote: > Thanks for your reply! > > Like I said, it's a multidimensional array, so I suppose that the > contents aren't primitive and I need to do as you suggested. Is there > a built-in function for this, or do I need to make my own. It seems > like a pretty common operation... You need to do it yourself. It may be pretty common, but the exact specifics of how deep to go etc... tend to be rather application dependant. Fred > > > Jordi > > On 24 dec, 16:50, Bryan Duxbury wrote: >> What's your array an array of? Array.dup will copy the array to a new >> array, but if the values in the array are references to some class >> you're using, it'll be a different array of the same items. >> Primitives would be copied correctly. You need to do your own deep >> copy if it's a more complicated array value. >> >> -Bryan >> >> On Dec 24, 2007, at 7:19 AM, jbie...@gmail.com wrote: >> >>> Hi, I'm a total newbie with Ruby and I was trying to make this >>> function that would get a (multidimensional) array, make a copy, >>> make >>> some changes to the copy and then return the copy, without altering >>> the original array. However, no matter what I try, the original >>> array >>> gets altered. Here is my code: >> >>> def prune_times (times) >>> # copy = times >>> # copy = Array.new(times) >>> # copy = times.clone >>> copy = times.dup >>> copy.each do |map| >>> map.each do |task| >>> task.replace(best_n(task, 2)) >>> end >>> end >>> return copy >>> end >> >>> Any help would be greatly appreciated! >> >>> Jordi > >