Ruby code problem

  • From: "Sean Murphy" <smurf_bp@xxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Sun, 13 Jan 2008 16:58:30 +1100

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

Other related posts: