0

I have an array

Numbers =[ [ [ [1], [2] ], [ [3], [4] ], ], [ [ [5], [6] ], [ [7], [8] ] ] ] 

I want to get the results like this

[ [ [1], [2] ],[ [3], [4] ]] 

and

[ [ [5], [6] ],[ [7], [8] ]] 

in Ruby.

Is that possible?

Python equivalent is

for Number in Numbers: print Number 

2 Answers 2

3

Use each and inspect:

Numbers.each { |n| puts n.inspect } 

For example:

>> Numbers.each { |n| puts n.inspect } [[[1], [2]], [[3], [4]]] [[[5], [6]], [[7], [8]]] 

BTW, technically you have an array of arrays or arrays, there are no multi-dimensional Arrays in Ruby (unless you create your own class to implement them of course).

Sign up to request clarification or add additional context in comments.

4 Comments

Isn't a multidimensional array essentially just an array of arrays anyway?
@AndrewMarshall: No, a multidimensional array is different. The rows of a 2D array all have the same length (as do the columns) but that doesn't necessarily hold in an AoA; an AoA is not a matrix. The distinction is a bit pedantic I admit.
Ah I see what you're saying, that makes sense. Somehow the length didn't occur to me, probably since they're all usually the same length anyway in most situations. Nothing wrong with being pedantic!
BTW, "puts n.inspect" is equivalent to "p n", just saying
1

Equivalent to Python style:

for number in Numbers do p number end #=> [[[1], [2]], [[3], [4]]] [[[5], [6]], [[7], [8]]] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.