From: Robert Klemme Date: 2009-11-19T23:02:14+09:00 Subject: Re: dynamic array? 2009/11/19 Freak Guard : > I'd like to store 2d-data into an dynamic Array. The Matrix class got > the problem of being static sized and the other solution does not work: > > You can pass a block to Hash#new which gets called if the key does not > exist > >>> hsh = Hash.new {[]} > => {} >>> hsh[3] > => [] > > But this does not seem to work with Arrays... > >>> ary = Array.new {[]} > => [] >>> ary[2] > => nil So basically you need a lazy Matrix? Or do you need a matrix which accepts arbitrary indexes? In 1.8.7 and newer this is as easy as class LazyM def initialize(default = nil) @h = Hash.new(default) end def [](x,y) @h[[x,y]] end def []=(x,y,z) @h[[x,y]]=z end def clear @h.clear end end Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/