From: Daniel DeLorme Date: 2007-06-25T08:06:44+09:00 Subject: Re: Helper to create multi-dimensional arrays Anthony Martinez wrote: > I came up with this method really quick to create x*y arrays in Ruby. It > mimicks Array#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 << Array 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