Aditya Mahajan
6/28/2007 9:09:00 PM
On Jun 24, 6:06 pm, Daniel DeLorme <dan...@dan42.com> 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 <<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
This is great. Finally a sensible way to create multi-dimensional
arrays in Ruby. Thank you Anthony and Daniel.
Aditya