The latest solution is, indeed, more Rubyish. Your previous code did work, however, after moving a line in the way I described. Jamal On Tue, 15 Jan 2008, Sean Murphy wrote: > Date: Tue, 15 Jan 2008 20:23:23 +1100 > From: Sean Murphy <smurf_bp@xxxxxxxxxx> > Reply-To: programmingblind@xxxxxxxxxxxxx > To: programmingblind@xxxxxxxxxxxxx > Subject: Re: Ruby code problem > > Hi, > > thanks for the response. I found the problem in the original code. I was > not re-initialising the col array. The below code is how I should be doing > my example: > > def make_2d_array n_row, n_col > Array.new(n_row) do > Array.new(n_col){ rand(2) } > end > end > > make_2d_array(gets.to_i, gets.to_i) > > > The " Array.new(n_row) do > " creates the primary array and sets up a code block. Depending on the > number of elements I want to create for Row determines how many times the > code block is executed. The line which creates the col array, does the same > thing as previously described and executes the random method code block. > The value from the Random method is assigned to the new element for col. > Once all the columns are created, the col array is assigned to the row > array. Then the whole process is repeated until all the number of elements > defined to row have been created. > > I hope this helps others and I didn't come up with the solution either. I > went to the Ruby mail list and receive a good answer from there. > > Regards > Sean Murphy > Skype: smurf20005 > > Life is a challenge, treat it that way. > ----- Original Message ----- > From: "Jamal Mazrui" <empower@xxxxxxxxx> > To: <programmingblind@xxxxxxxxxxxxx> > Sent: Tuesday, January 15, 2008 3:22 AM > Subject: Re: Ruby code problem > > > > Try moving the line > > row = Array.new > > below the line > > while (counter_x < x) > > > > I think the row array continued to grow instead of being re-initialized > > for each column array. Each item in the column array contained a > > reference to the same row array, so their values were the same. > > > > Jamal > > > > On Sun, 13 Jan 2008, Sean Murphy wrote: > > > >> Date: Sun, 13 Jan 2008 16:58:30 +1100 > >> From: Sean Murphy <smurf_bp@xxxxxxxxxx> > >> Reply-To: programmingblind@xxxxxxxxxxxxx > >> To: programmingblind@xxxxxxxxxxxxx > >> Subject: Ruby code problem > >> > >> All, > >> > >> I am trying to create a two dimension array in Ruby. The books I have > >> referred two doesn't give me any examples on how to create a two > >> dimension > >> array by a programmatic way. They do talk about creating it by hard > >> code. > >> > >> The below method is trying creates a 3 by 3 array with a random number > >> between 0 to 1. > >> > >> def create_world x, y > >> col = Array.new > >> row = Array.new > >> counter_x = 0 > >> counter_y = 0 > >> > >> while (counter_x < x) > >> puts 'x ' + counter_x.to_s.to_s > >> while (counter_y < y) > >> puts 'y ' + counter_y.to_s > >> map_type = rand(2) > >> puts 'Random: ' + map_type.to_s > >> row.push map_type > >> counter_y = counter_y + 1 > >> end > >> col.push row > >> counter_y = 0 > >> counter_x = counter_x + 1 > >> end > >> return col > >> end > >> > >> world = create_world(3, 3) > >> puts world.to_s > >> puts 'array ' + world[0].size.to_s > >> > >> Problem with code above is: > >> > >> 1. I get 3 rows. But in each row I get 9 columns. > >> 2. Each row content is the same. > >> > >> So can anyone help with the above? > >> > >> Regards > >> Sean Murphy > >> Skype: smurf20005 > >> > >> Life is a challenge, treat it that way. > >> > >> __________ > >> View the list's information and change your settings at > >> //www.freelists.org/list/programmingblind > >> > > __________ > > View the list's information and change your settings at > > //www.freelists.org/list/programmingblind > > > > __________ > View the list's information and change your settings at > //www.freelists.org/list/programmingblind > __________ View the list's information and change your settings at //www.freelists.org/list/programmingblind