Todd Benson
2/2/2008 9:02:00 PM
On Feb 2, 2008 11:55 AM, Adam Akhtar <adamtemporary@gmail.com> wrote:
> Hi im just coming to ruby from C albeit after many years of no
> programming. Im trying the simple matrix problem to get used to dealing
> with arrays in ruby.
>
> How do you create a blank 2d array?
>
> I want to ask the user for the number of rows and columns and then
> create a blank 2d array using those dimensions. From there I'd populate
> it with the users desired data.
>
> Cheers
x, y = 2, 3 #you get these values from the user
m = [] #initializing m for scope reasons
x.times { m << Array.new( y ) } # adding new arrays to m
That should get you started. Keep in mind that arrays are just
ordered lists of objects. You can have an ordered list of any object
(including other arrays). The [] index call can be chained. So to
access this 2d array at 0,0 you would do m[0][0].
hth,
Todd