Rajasankar
Rajasankar

Reputation: 928

Accessing array elements in Ruby

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

Upvotes: 0

Views: 148

Answers (2)

mu is too short
mu is too short

Reputation: 434965

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).

Upvotes: 3

megas
megas

Reputation: 21791

Equivalent to Python style:

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

Upvotes: 1

Related Questions