From: ara.t.howard@... Date: 2006-10-12T01:10:57+09:00 Subject: Re: PHP-like Array/Hash creation in Ruby On Thu, 12 Oct 2006, Jan Reitz wrote: > my problem is that if i have an multi dimensional array, and it needs to > grow (by itself if i assign values outside of the allocated space), the > newly created will be "nil" so i cant use my own class for that. > > in c# 2.0 i would write > List> mda = new List>() > mda[99][99] = "test"; > > Tada, tested it and it does _not_ work, lol..... > > BUT what hinders ruby to be better than that.... > > the question i ask is is it doable ? can someone help solving ? im stuck > with this "cant reassign self" or whatever it said. harp:~ > cat a.rb class MArray < Array class DimProxy def initialize(d, i) @d, @i = d, i end def inspect () @d.inspect << " [[ #{ @i } ]]" end def dp(i) DimProxy.new self, i end def [](i) dp i end def []= i, v (a = [])[i] = v @d[@i] = a v end end def [](i) super or dp i end def dp(i) DimProxy.new self, i end end ma = MArray.new ma[0][1][2] = 42 puts "ma[0][1][2] : #{ ma[0][1][2].inspect }" puts "ma[0][1] : #{ ma[0][1].inspect }" puts "ma[0] : #{ ma[0].inspect }" puts "ma : #{ ma.inspect }" harp:~ > ruby a.rb ma[0][1][2] : 42 ma[0][1] : [nil, nil, 42] ma[0] : [nil, [nil, nil, 42]] ma : [[nil, [nil, nil, 42]]] -a -- my religion is very simple. my religion is kindness. -- the dalai lama