我誤會陣列行爲紅寶石陣奇怪的行爲
當我創造了這個矩陣
matrix, cell = [], []; 5.times { cell << [] } # columns
3.times { matrix << cell } # lines
matrix
sample_data = (0..5).to_a
matrix[1][2] = sample_data.clone
matrix.each { |line| puts "line : #{line}" }
我有這樣的結果
line : [[], [], [0, 1, 2, 3, 4, 5], [], []]
line : [[], [], [0, 1, 2, 3, 4, 5], [], []]
line : [[], [], [0, 1, 2, 3, 4, 5], [], []]
而不是預期的結果
line : [[], [], [], [], []]
line : [[], [], [0, 1, 2, 3, 4, 5], [], []]
line : [[], [], [], [], []]
什麼錯了?
或者因爲單元格是空的,只需使用新的[]而不是指定單元格......'3times {matrix << []}' – DGM