I have an object like this
class Place
def initialize(x,y)
@x = x
@y = y
end
end
So, I have to emulate a dynamic grid with size X,Y. For example 3x5 (width, height).
I need to save all the "Place" objects in the array, with all the possible values, Place(1,1), Place(1,2), Place(1,3)... Place(3,5).
I tried this way (inside Grid)
def createPlaces width, height
@places = Array.new(width * height)
(0..width).to_a.each do |x|
(0..height).to_a.each do |y|
@places.push(Place.new(x,y))
end
end
end
But is not working. How can I do it?
0...width, where that is equivalent to0..width-1. Width 10 would mean 11 entries in your array with your code, which is wrong.