From: ahoward Date: 2002-11-10T09:23:21+09:00 Subject: Re: Multi-dimensional (like 2) arrays in Ruby On Sun, 10 Nov 2002, Pit Capitain wrote: [snip] P> Please see the other posts to this thread for ways to improve the P> code. whoops! my bad. here's a fix class Array def Array.Array dims, default=nil dim = dims.shift if dims.size == 0 return Array.new dim, default else a = Array.new dim a.collect!{ Array.Array dims.clone, default } end end end if $0 == __FILE__ m = Array.Array [3,2,1], 0 p m # >> [[[0], [0]], [[0], [0]], [[0], [0]]] m[0][0][0] = 42 m[1][0] = [42] m[2] = [[42],[0]] p m # >> [[[42], [0]], [[42], [0]], [[42], [0]]] end note that this works with dims.size == n -a -- ==================================== | Ara Howard | NOAA Forecast Systems Laboratory | Information and Technology Services | Data Systems Group | R/FST 325 Broadway | Boulder, CO 80305-3328 | Email: ahoward@fsl.noaa.gov | Phone: 303-497-7238 | Fax: 303-497-7259 ====================================