How do you create your own multidimensional array?

45 views Asked by At

I am coding and I came across this problem in my code, How do you create your own multidimensional array in Ruby? I tried searching up but none of the websites could help.

1

There are 1 answers

1
Aleksei Matiushkin On

There are no multidimentional arrays in Ruby (at least they are not first class citizens.)

One might simulate this behavior by creating an array containing arrays of the same length:

arr = [[1, 2], [3, 4]]

or, dynamically:

arr = Array.new(2) { |_| Array.new(2) } 

Also, there is Matrix class in standard library.