From: timr Date: 2009-12-29T15:15:05+09:00 Subject: Re: PLEASE HELP...dup is not working correctly in the following code On Dec 28, 9:41 pm, Seebs wrote: > On 2009-12-29, timr wrote: > > > However, unlike the irb session, the following code shows that > > modification of the dupped object is somehow changing the original. > > No, it isn't. > > You have two separate arrays. > > However, each of those arrays contains the same strings -- the first string > of each array is the same object. > > You want a deep copy (where you duplicate all the objects in the array, > not just the arrays).  Something like: > > @maze_dup = @maze.map { |x| x.dup } > > ... I think. You are right about the strings having the same object_ids in each object, which explains why the modification on the dupped object affects the original strings. That helps. But the suggested strategy doesn't solve the problem: >> a = [['a', 'b'], ['c','d']] => [["a", "b"], ["c", "d"]] >> a.each{|r| r.each{|letter| p letter.object_id}} 4442750 4442740 4442710 4442700 => [["a", "b"], ["c", "d"]] >> b = a.map{|r| r.each{|letter| letter.dup}} => [["a", "b"], ["c", "d"]] >> b.each{|r| r.each{|letter| p letter.object_id}} 4442750 4442740 4442710 4442700 Still the same object_ids for all of the contents. Tim > > -s > -- > Copyright 2009, all wrongs reversed.  Peter Seebach / usenet-nos...@seebs.nethttp://www.seebs.net/log/<-- lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!