Hi --

On Thu, 1 Aug 2002, Tom Sawyer wrote:

> funny you should ask. i just had to do that myself.
>
>           aT = []
>           a.length.times do |y|
>             a[y].length.times do |x|
>               aT[x] = [] unless aT[x]
>               aT[x][y] = a[y][x]
>             end
>           end
>
> the only thing somewhat annoying about this is having to specify aT[x] =
> []. wish ruby could just pick up on that when i try to use it as such.

There's nothing for Ruby to pick up on.  If Ruby's first exposure to
a variable is:

   var[10] = "hi"

all it knows is that var responds to a method called #[]= (where
var[10]="hi" is short for var.[]=(10,"hi")).  There's absolutely no
indication that var is supposed to be an Array (or a Hash, or a
String, or a NewThingy).

However... there's a more concise way to do the "initialize if
necessary" thing:

  aT ||= []

which is quite a common Ruby idiom.


David

-- 
David Alan Black
home: dblack / candle.superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav