From: Martin DeMello Date: 2002-12-09T23:29:41+09:00 Subject: Re: multi-dimension array Shannon Fang wrote: > Hi Guru, >> > >> > You're doing too much work -- let Ruby do it :-) >> > >> > arr = (1..3).map { [0,0] } >> > > > That's really neat. Thanks for show us the ruby way. Here's a quick extension to several dimensions, using Joel VanderWerf's enum tools ( http://www.ruby-lang.org/raa/list.rhtml?id=657 ) require 'enum/tree' require 'enum/op' class << Array def n_dim(*args) return (1..args[0]).to_a.map! {nil} if args.length == 1 a = args.shift (1..a).to_a.map! {n_dim(*args)} end end class Array def deep_each self.by_depth { |x| x.kind_of?(Array) ? x : [] }.each {|i| yield i unless i.kind_of?(Array) } end end include EnumerableOperator a = Array.n_dim(2,3,4) product(0..1, 0..2, 0..3).each {|i,j,k| a[i][j][k] = "(#{i},#{j},#{k})" } a.deep_each {|i| p i}