Is there any specific reason you would want to write your own array class? By default, you can tell array what to populate the new elements with, by providing the second argument:
>> a = Array.new(10, [])
=> [[], [], [], [], [], [], [], [], [], []]
Edit: Apparently, this way it populates the arrays with references to passed object, so once you do a[0][0] = "asd"
, every first element of contained arrays will change. Not cool.
>> a[0][0] = "asd"
=> "asd"
>> a
=> [["asd"], ["asd"], ["asd"], ["asd"], ["asd"], ["asd"], ["asd"], ["asd"], ["asd"], ["asd"]]
To have each contained array be unique, use the third syntax, and give it a block to execute each time - the result of the block will be used to populate the array:
>> b = Array.new(10) { [] }
=> [[], [], [], [], [], [], [], [], [], []]
>> b[0][0] = "asd"
=> "asd"
>> b
=> [["asd"], [], [], [], [], [], [], [], [], []]
Also, due to the way ruby arrays work, defining y axis size is not even necessary:
>> a = Array.new(5)
=> [nil, nil, nil, nil, nil]
>> a[10]
=> nil
>> a[10] = "asd"
=> "asd"
>> a
=> [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, "asd"]
The array is automatically extended when you put something in index that is larger than current size. So, just make an array containing ten empty arrays, and you have 10*n sized array ready for use.