I am trying to iterate a multidimension array created with the following line
To iterate i'm using the following code
visiblematrix= Array.new (10) {Array.new(10){0}}
But this doesn't allow me to know the current x,y position while iterating. how can i find it out without resorting to temporary variables
visiblematrix.each do |x|
x.each do |y|
puts y
end
end
You can also use the Enumerable#each_with_index method (ruby arrays include the Enumerable mixin).
visiblematrix.each_with_index do |x, xi|
x.each_with_index do |y, yi|
puts "element [#{xi}, #{yi}] is #{y}"
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With