Multi-dimensional (like 2) arrays in Ruby

Choices, choices!

All of the examples worked. Now which one should I use?

My gut instinct is to take the first one. It has no methods attached to it and seems clear as to what it is doing.
Is this correct and reasonable? Or are the methods “hidden”?

@matrix = Array.new(@rows, Array.new(@cols)) #+Works!

@M = (0…@rows).map {|r| Array.new(@cols,0)} #+Works!

@M = Array.new(@rows) #+Works!

0.upto(@rows-1) {|i| @M[i] = Array.new(@cols,i)}#+

@m = (0…@rows).collect {Array.new(@cols,0)}#+Works

@M = (0…@rows).map {Array.new(@cols,0)}#+Works

@m = (0…@rows).collect! {Array.new(@cols,0)}#+Works

@M = (0…@rows).map! {Array.new(@cols,0)}#+Works

I found that map == collect, but that I get an ‘undefined method collect!’ on the bang version…

“Ted” ted@datacomm.com writes:

Choices, choices!

All of the examples worked. Now which one should I use?

My gut instinct is to take the first one. It has no methods attached to it and seems clear as to what it is doing.
Is this correct and reasonable? Or are the methods “hidden”?

@matrix = Array.new(@rows, Array.new(@cols)) #+Works!

doesn’t work or it appears to work, but it is not what you want.

YS.