Xeno Campanoli wrote:
> There are just too many problems that can quickly be solved by 
> multi-dimensional arrays and hashes not to have them and have them 
> easily.  I used the hack from "matz" today to make a 2d hash, and it's 
> ugly and unworthy of Ruby. You should be able to do this without any 
> extra steps just like in Perl.  If the project ends up getting bigger 
> THEN you refactor it, but YOU JUST GOT TO HAVE THAT.

Well, the support for multiple dimensions of index selector is already 
there. That is, you already have a[a][b][c][d][e][f][g][h][i]..., you 
just need to create the array to start with. For that part, it's not too 
hard to write a convenient multi-dimensional array factory method:

require 'pp'

def ArrayFactory(dimensions, initialvalue)
   size = dimensions.first
   if dimensions.length == 1
     return Array.new(size, initialvalue)
   else
     rest = dimensions[1,dimensions.length-1]
     a = Array.new(size)
     for i in 0..size
       a[i] = ArrayFactory(rest, initialvalue)
     end
     return a
   end
end

a = ArrayFactory([3, 5, 7], "Hi!")
pp a

a[2][3][7] = "Hello!"
puts a[2][3][7]
pp a[2][3]

I have to say, though, that I've never had much need for 
multi-dimensional arrays. Outside the realm of scientific data analysis, 
most of the time you really want hashes because your "array" is sparse, 
has unusual bounds, or whatever. At that point you should probably be 
looking at writing a proper storage class.


mathew
-- 
<URL:http://www.pobox.com/~meta/>
          WE HAVE TACOS