#dup creates a copy of an object with a different object_id. As

follows:

a = [1,2,3]

=> [1, 2, 3]

b = a.dup

=> [1, 2, 3]

b << 1000

=> [1, 2, 3, 1000]

a

=> [1, 2, 3]

b

=> [1, 2, 3, 1000]

Changing b does't change a because b is a copy of a with a different

object_id.

However, unlike the irb session, the following code shows that

modification of the dupped object is somehow changing the original.

WTF? (you may want to change the font to courier to get the maze to

line up correctly). This behavior is so strange. I can see no

significant differences between the usage in the irb session and in

this code, yet the behavior is very different. Please explain if you

have any insights.

Thanks,

Tim

@maze1 = %{#####################################

# # # #A # # #

# # # # # # ####### # ### # ####### #

# # # # # # # # #

# ##### # ################# # #######

# # # # # # # # #

##### ##### ### ### # ### # # # # # #

# # # # # # B# # # # # #

# # ##### ##### # # ### # # ####### #

# # # # # # # # # # # #

# ### ### # # # # ##### # # # ##### #

# # # # # # #

#####################################}

class Maze

attr_accessor :maze

#initialize creates a @maze array with each row a sub array

def initialize(maze_name)

@maze = maze_name.split(/\n/).collect{|row| row.split(//)}

@maze_dup = @maze.dup

end

def write_(r, c)

@maze_dup[r][c] = "a"

end

def report

puts "@maze:"

@maze.each{|r| p r.join}

puts "@maze_dup:"

@maze_dup.each{|r| p r.join}

end

end

maze1 = Maze.new(@maze1)

maze1.report

maze1.write_(0,0)

maze1.report

maze1.write_(1,1)

maze1.report

# >> @maze:

# >> "#####################################"

# >> "# # # #A # # #"

# >> "# # # # # # ####### # ### # ####### #"

# >> "# # # # # # # # #"

# >> "# ##### # ################# # #######"

# >> "# # # # # # # # #"

# >> "##### ##### ### ### # ### # # # # # #"

# >> "# # # # # # B# # # # # #"

# >> "# # ##### ##### # # ### # # ####### #"

# >> "# # # # # # # # # # # #"

# >> "# ### ### # # # # ##### # # # ##### #"

# >> "# # # # # # #"

# >> "#####################################"

# >> @maze_dup:

# >> "#####################################"

# >> "# # # #A # # #"

# >> "# # # # # # ####### # ### # ####### #"

# >> "# # # # # # # # #"

# >> "# ##### # ################# # #######"

# >> "# # # # # # # # #"

# >> "##### ##### ### ### # ### # # # # # #"

# >> "# # # # # # B# # # # # #"

# >> "# # ##### ##### # # ### # # ####### #"

# >> "# # # # # # # # # # # #"

# >> "# ### ### # # # # ##### # # # ##### #"

# >> "# # # # # # #"

# >> "#####################################"

# >> @maze:

# >> "a####################################"

# >> "# # # #A # # #"

# >> "# # # # # # ####### # ### # ####### #"

# >> "# # # # # # # # #"

# >> "# ##### # ################# # #######"

# >> "# # # # # # # # #"

# >> "##### ##### ### ### # ### # # # # # #"

# >> "# # # # # # B# # # # # #"

# >> "# # ##### ##### # # ### # # ####### #"

# >> "# # # # # # # # # # # #"

# >> "# ### ### # # # # ##### # # # ##### #"

# >> "# # # # # # #"

# >> "#####################################"

# >> @maze_dup:

# >> "a####################################"

# >> "# # # #A # # #"

# >> "# # # # # # ####### # ### # ####### #"

# >> "# # # # # # # # #"

# >> "# ##### # ################# # #######"

# >> "# # # # # # # # #"

# >> "##### ##### ### ### # ### # # # # # #"

# >> "# # # # # # B# # # # # #"

# >> "# # ##### ##### # # ### # # ####### #"

# >> "# # # # # # # # # # # #"

# >> "# ### ### # # # # ##### # # # ##### #"

# >> "# # # # # # #"

# >> "#####################################"

# >> @maze:

# >> "a####################################"

# >> "#a# # #A # # #"

# >> "# # # # # # ####### # ### # ####### #"

# >> "# # # # # # # # #"

# >> "# ##### # ################# # #######"

# >> "# # # # # # # # #"

# >> "##### ##### ### ### # ### # # # # # #"

# >> "# # # # # # B# # # # # #"

# >> "# # ##### ##### # # ### # # ####### #"

# >> "# # # # # # # # # # # #"

# >> "# ### ### # # # # ##### # # # ##### #"

# >> "# # # # # # #"

# >> "#####################################"

# >> @maze_dup:

# >> "a####################################"

# >> "#a# # #A # # #"

# >> "# # # # # # ####### # ### # ####### #"

# >> "# # # # # # # # #"

# >> "# ##### # ################# # #######"

# >> "# # # # # # # # #"

# >> "##### ##### ### ### # ### # # # # # #"

# >> "# # # # # # B# # # # # #"

# >> "# # ##### ##### # # ### # # ####### #"

# >> "# # # # # # # # # # # #"

# >> "# ### ### # # # # ##### # # # ##### #"

# >> "# # # # # # #"

# >> "#####################################"