From: Matthew Margolis Date: 2004-09-21T11:02:15+09:00 Subject: Re: Filling a 2D array Mehr, Assaph (Assaph) wrote: >>>Gah, posted too soon! >>> >>># initialize an array of arrays: >>>@tileArray = Array.new(@height) { Array.new } >>># Initizlize arrays of tiles: >>>@height.times { @tileArray.push Array.new(@width) {Tile.new} } >>> >>> > > > >>Ah, that makes sense. >> >> > >I should REALLY get coffee before I post anything more today! > >If you use my example above you'll end up with @tileArray being an Array >with the first @height elements being empty Arrays and the second >@height arrays being the tile arrays. The first initialization should >just be > @tileArray = [] >So you could use the push method in the block. However, it's probably >cleaner (and a bit more efficient) to use: > @tileArray = Array.new(@width) { Array.new(@height) {Tile.new} } > >Thus the first parameter is the number of elements in the array and the >second is a block to call for each cell, for both rows and columns. > >I'll go get coffee now. > > > Excellent. This time I have run a few tests before responding and have verified that your advice is good. -Matthew Margolis