From: Martin DeMello Date: 2005-10-12T00:21:52+09:00 Subject: Re: multi-dimensional Arrays Peter v. N. wrote: > > I know there exist "simple" ways (Google's thy friend) > to create md arrays with Ruby. But it's somehow cumbersome > and it would be nice if it could be done like "array = Array.new(3,3)" > -> I know too that this means not the same in Ruby... > > What I like to know: what led to the decission to do it that way > and not like it is done in the C/C++ (Java) languages � la > "int[][] arr = new int[3][3]"? There are several different kinds of multidimensional arrays. One is true rectangular arrays (efficient memory allocation, bounds checked in every dimension) - I think FISh supports those[1], and probably Matlab as well. Then there's C's rectangular arrays with weak (no!) bounds checking - the array is allocated as a contiguous block, and multidimensional indexes are converted into (computed) single dimensional offsets into the block. And finally, Ruby uses the lists-of-lists approach, in which you have only one-dimensional arrays, but every cell of those arrays can be an array itself (and so on recursively). Not as efficient as the rectangular approach, but it allows for irregularly shaped arrays. martin [1] http://www-staff.it.uts.edu.au/~cbj/FISh/Announcement/