traverse two-dimesional array in ruby

A

aidy

Hi,

I know I can use the each method to traverse a one-dimensional array

my_array.each{|x|
p x
}

but how would I do a similar thing for a two dimensional array?

cheers

aidy
 
D

Dumaiu

Once you have the 'nested each()' trick down, you realize that this
is basically what Array#flatten() does. As long as efficiency isn't
paramount, you can shortcut with 'array.flatten.each {}' for any depth.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,208
Messages
2,571,080
Members
47,682
Latest member
TrudiConna

Latest Threads

Top