From: Todd Benson Date: 2008-02-03T06:02:14+09:00 Subject: Re: creating a 2d array On Feb 2, 2008 11:55 AM, Adam Akhtar 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