當我嘗試從一個數組流行元素紅寶石流行陣列,它彈出。當我分配該陣列另一個變量彈出之前,然後,如果我彈出,彈出操作會影響二者的陣列。而不影響其它變量
例如:
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 >
如果我使用的流行,所有的變量都受到影響。我想保留原始數組並彈出另一個。怎麼樣?
你的編輯是一個不同的問題。請打開一個新問題。 – Mischa
@Mischa:問了。感謝您的支持! – beck03076