I am working on array issues outlined at http://prepwork.appacademy.io/mini-curriculum/array/
I am trying to create a function my_transpose that takes in a matrix and returns its transpose.
I am very confused about writing a 2D array! Here is a code snippet that highlights my confusion.
rows = [ [0, 1, 2], [3, 4, 5], [6, 7, 8] ] columns = Array.new(3 , Array.new(3)) puts columns.to_s #Output is a 3 x 3 array filled with nil columns[0][0] = 0 puts columns.to_s #Output is [[0,nil,nil], [0,nil,nil], [0,nil,nil]]
Why does changing columns [0] [0] change all three variables? Shouldn't it just change the first cell in the first row?
source share