当我尝试从数组中弹出一个元素时,它会弹出。当我在弹出之前将该数组分配给另一个变量,然后如果我弹出,弹出操作会影响两个数组。
例如:
ruby-1.9.2-p290 :339 > a= [1,2,3]
=> [1, 2, 3]
ruby-1.9.2-p290 :340 > b = a
=> [1, 2, 3]
ruby-1.9.2-p290 :341 > a
=> [1, 2, 3]
ruby-1.9.2-p290 :342 > b
=> [1, 2, 3]
ruby-1.9.2-p290 :343 > a.pop
=> 3
ruby-1.9.2-p290 :344 > a
=> [1, 2]
ruby-1.9.2-p290 :345 > b
=> [1, 2] #WHY?
ruby-1.9.2-p290 :346 > x = [1,2,3]
=> [1, 2, 3]
ruby-1.9.2-p290 :347 > y = x
=> [1, 2, 3]
ruby-1.9.2-p290 :348 > z = x
=> [1, 2, 3]
ruby-1.9.2-p290 :349 > y
=> [1, 2, 3]
ruby-1.9.2-p290 :350 > z
=> [1, 2, 3]
ruby-1.9.2-p290 :351 > y.pop
=> 3
ruby-1.9.2-p290 :352 > y
=> [1, 2]
ruby-1.9.2-p290 :353 > z
=> [1, 2] # WHY?
ruby-1.9.2-p290 :354 > x
=> [1, 2]
ruby-1.9.2-p290 :355 >
如果我使用pop,所有变量都会受到影响。如何保留原始数组并仅从另一个数组中弹出?