From: Tamreen Khan Date: 2007-01-17T03:56:47+09:00 Subject: Re: Array Default Values ------=_Part_60290_12135892.1168973805708 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 1/16/07, David Goodlad wrote: > > On 1/16/07, Drew Olson wrote: > > I'd like to have my array behave like this: If I add an item to a[5], > > a[0..5] will be equal to " " rather than nil. I tried to following: > > > > irb(main):001:0> a=[" "] > > => [" "] > > irb(main):002:0> a[5] = "test" > > => "test" > > irb(main):003:0> a.inspect > > => "[\" \", nil, nil, nil, nil, \"test\"]" > > > > Is there any way to have those nils default to " "? > > You can also do: a=[" "] x=5 (x-1).times do |x| a[x]=" " end It's a bit more code, but also more flexible, filling up everything up to and not including 5. ------=_Part_60290_12135892.1168973805708--