While each
does not change the values of a given row in a 2d array (unlike map
):
test_array = [[0,0],[0,0],[0,0]]
test_array[0].each {|e| e = 1}
test_array # => [[0, 0], [0, 0], [0, 0]]
test_array[0].map! {|e| e = 1}
test_array # => [[1, 1], [0, 0], [0, 0]]
each
changes the values of a given column (in a way different from map
):
test_array = [[0,0],[0,0],[0,0]]
test_array.each {|row| row[0] = 2}
test_array # => [[2, 0], [2, 0], [2, 0]]
test_array.map! {|row| row[0] = 2}
test_array # => [2, 2, 2]
Can somebody explain what is happening?