Nuno Furtado Nuno Furtado - 1 year ago 64
Ruby Question

Iterating over two dimension array and knowing current position

I am trying to iterate a multidimension array created with the following line

To iterate i'm using the following code

visiblematrix= (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

Answer Source

use each_index instead of just each.

Keep in mind x and y would now be your index not the value at that index. So visiblematrix[x] etc.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download