23

Can somebody please tell me how to declare a new instance of a 2-dimensional array? Most of the languages use something like:

array = Array.new[2][2]

I don't know how to do it in Ruby.

1
  • 2
    That would be quite tedious if he wanted the height and width to be 1000, for example. Commented Mar 21, 2011 at 0:57

5 Answers 5

48

You can do:

width = 2
height = 3
Array.new(height){Array.new(width)} #=> [[nil, nil], [nil, nil], [nil, nil]] 
Sign up to request clarification or add additional context in comments.

1 Comment

Traditionally arrays are layed out in row major order, ie [row, row row]. So your example should swap height and width so that it's like this: Array.new(height){Array.new(width)}. That way you can access an element using arrayInstance[row][column] instead of arrayInstance[column][row]
11

To declare 2d array in ruby, Use following syntax with initialization value

row, col, default_value = 5, 4, 0
arr_2d = Array.new(row){Array.new(col,default_value)}
=> [[0, 0, 0, 0], 
    [0, 0, 0, 0], 
    [0, 0, 0, 0], 
    [0, 0, 0, 0], 
    [0, 0, 0, 0]]

We can do any level of nesting, like for 3d array(5 x 4 x 2): you can pass block to initialize array in most inner Array

z = 2
arr_3d = Array.new(row){Array.new(col){Array.new(z){|index| index}}}
=> [[[0, 1], [0, 1], [0, 1], [0, 1]], 
    [[0, 1], [0, 1], [0, 1], [0, 1]], 
    [[0, 1], [0, 1], [0, 1], [0, 1]], 
    [[0, 1], [0, 1], [0, 1], [0, 1]], 
    [[0, 1], [0, 1], [0, 1], [0, 1]]]

Now, you can access its element using [] operator like arr_2d[0][1], actually its array of arrays

2 Comments

Didn't know you could specify a default element for an array. So you can specify the inner array as the default for the outer array too: s = Array.new(3, Array.new(3, true)) => [[true, true, true], [true, true, true], [true, true, true]] Oh, no, you can't. The same array is used for each row. Fail.
Use {} instead of () while declaring the inner array, it will work
4

You could also initialize passing a value:

Array.new(3) { Array.new(3) { '0' } }

Output:

[
 ["0", "0", "0"], 
 ["0", "0", "0"], 
 ["0", "0", "0"]
]

Comments

3

You can declare a multidimensional array in Ruby with:

Array.new(Number_of_ROWs){Array.new(Number_of_COLUMNs)}

How To Use This Syntax

Let us understand it by using above example i.e. array = Array.new[2][2].

So, in this example we've to declare an empty multidimensional array with 2 rows and 2 column.

Let us start implementing the our syntax now,

array = Array.new(2){Array.new(2)}

Now you've an array with 2 rows and 2 column with nil values.

Now the array variable contains [[nil, nil], [nil, nil]] which is consider as an empty multidimensional array or nil value multidimensional array.

Comments

-1

simply: array = Array.new(8,Array.new(8))

1 Comment

This doesn't really work. This would create 8 copies of the same array, but not 8 distinct arrays

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.