From: adi Date: 2007-06-29T06:10:05+09:00 Subject: Re: Helper to create multi-dimensional arrays On Jun 24, 6:06 pm, Daniel DeLorme wrote: > Anthony Martinez wrote: > > I came up with this method really quick to create x*y arrays in Ruby. It > > mimicksArray#new's behavior pretty closely wrt blocks, I hope. What do you all > > think? > > What about making it more general than just x*y arrays? > > class < def multi(n, *args, &block) > if args.empty? > Array.new(n, &block) > else > Array.new(n) do > Array.multi(*args, &block) > end > end > end > end > > ?>Array.multi(2){ 0 } > => [0, 0] > >>Array.multi(2,2){ 0 } > => [[0, 0], [0, 0]] > >>Array.multi(2,2,2){ 0 } > => [[[0, 0], [0, 0]], [[0, 0], [0, 0]]] > >>Array.multi(2,2,2,2){ 0 } > > hehe, playing around is fun > > Daniel This is great. Finally a sensible way to create multi-dimensional arrays in Ruby. Thank you Anthony and Daniel. Aditya