We have array of arrays: matrix = [[0, 1, 1, 2], [0, 5, 0, 0], [2, 0, 3, 3]]
Now, calling
matrix.each do |x|
print x
end
returns the three arrays as I would expect ([0, 1, 1, 2][0, 5, 0, 0][2, 0, 3, 3]).
But when doing
matrix.each do |x|
if x.include?(0)
z_index = x.index(0)
for zero in z_index
for x in matrix do
matrix[i].delete_at(zero)
i+=1
end
end
else
next
end
end
I get undefined method 'each' for 0:Integer, which means each is picking up the 0 in the first nested array, instead of picking up the entire first nested array. Why is it behaving this way?
#do something here codeit haseachloopfor zero in z_indexis the issue.z_indexis obviously0there. FYI: any iteration useseachunder the hood in ruby. Also, we don’t useforloops in ruby, it’s considered a code smell.