有人可以解释一下吗?
def digit_block(size = 1)
col = 2 + 1*size
row = 1 + 2*size
r = []
for i in 0...col
r.push ' '
end
a = []
for i in 0...row
a.push r
end
a
end
block = digit_block
puts block.inspect
block[1][2] = 'x'
puts block.inspect
输出:
[[" ", " ", " "], [" ", " ", " "], [" ", " ", " "]]
[[" ", " ", "x"], [" ", " ", "x"], [" ", " ", "x"]]
我的理解是 block[1][2] 仅更改第 1 行第 2 列的单元格,但为什么它会更改第 2 列中的所有单元格?