From: gotoken@... (GOTO Kentaro) Date: 2001-01-04T06:33:39+09:00 Subject: [ruby-talk:8596] Re: bug?? In message "[ruby-talk:8592] Re: bug??" on 01/01/04, Jean Michel writes: >It would be wonderful if you also add a >similar alternative constructor for Array. I could list two reasons: * Array has ordered index set (i.e. Integer) and substituion to a new position `i' will allocate for all posisions before `i'. So such default mechanism leads to many inefficient code parhaps. * We rarely need default mechanism for Array. Btw, you can implement by a Ruby code if you want that: class Array class << self alias __new__ new def new(*arg, &callback) ary = __new__(*arg) ary.instance_eval{@default_proc = callback} ary end end attr_reader :default_proc alias aref [] def [] (*arg) if arg.size == 1 i, = arg if default_proc and i.is_a? Integer and i+1 > size default_proc.call(i, self) end else aref *arg end end end a = Array.new{|i,a| a[i] = ""} a[3].concat "a" p a #=> [nil, nil, nil, "a"] Now, I can't decide we should fill by nil or "" as the elements before a[3] above. Furthermore, any behaviors of like a[0..3] or a[0,3] seem to be non-trivial if a block is given to Array.new. It may be the biggest reason why we consider not to propose that for Array. After all, Hash and Array are not so similar object, I thought. Thanks, -- Gotoken