From: Todd Benson Date: 2007-06-14T02:22:04+09:00 Subject: Re: Generate a grid of cells, and give characteristics On 6/13/07, Joop Van den tillaart wrote: > Do i need to put this array in some sort of database? So i get to have > some columns where one column represents the cells numbers 1..196 some > other columns then need to have some cell characteristics, and with > these characteristics and a formula (which i already have) the cell > types need to be assigned (infrastructure, housing, facilities etc.) What's your motivation? To just get it done and working (I'm a little confused as to what "it" is, though)? To learn Ruby or OOP? Is this a one-off (used just for this) or to be expanded and used for something else (are you building a script or an app, for example)? Are you automating data construction, manipulation, or evaluation, or all of the above? You say you have formulas for characteristics of the each of the 196 "plots". Do these formulas depend solely on x and y coordinates (i.e. all plots with x == 4 have a creek running through them, all plots with y == 13 are next to power lines, etc.) or do you plug this info in for each plot one at a time? Is it permanent info? Should you use a database? It depends. I'm a database freak, so I probably would use one even if it required some extra effort (but I'm well versed in SQL, so that part would be easy; there are some DB engineers out there that would be more than happy to encode your application entirely in the DB, complete with distance calculations, autogeneration of street addresses, the whole works! But that's no fun ... more like trying to fit a square peg in a round hole. By the sound of it so far, I'd start with a database containing the unique and rare characteristics of each plot and then "script" into the DB the more general characteristics using SQL and/or Ruby. Then I'd build some generic classes so that I can alter data in the database, maybe a Site class (of which you will have only one instance), a Plot class, a Road class, etc. If you want to use Ruby only, I recommend using YAML for your persistent data. Or, if you want to stick with a database but avoid SQL, there are some great ORM and Ruby/SQL bridge tools out there, like ActiveRecord and Sequel (neither of which I've tried). There are tons of ways to model this in Ruby; here's one cowboy way I might try with no regard to an external database -- just playing with data in the program: class Plot @@neighbors = [] attr_accessor :x, :y, :characteristics def @@neighbors.[]( x, y ) self.select {|n| n.x==x and n.y==y}.first end def @@neighbors.used?( x, y ) self[x,y] end def < new_chars @characteristics.merge! new_chars if new_chars.is_a? Hash self end def self.community @@neighbors end def initialize( x, y ) raise Exception.new("Property at (#{x},#{y}) already initialized!") if @@neighbors.used?( x, y ) @x = x @y = y @characteristics = {} @@neighbors << self end def to_s "Plot <#{@x},#{@y}>: #{@characteristics.inspect}" end end ##### # Begin little program to play with data # build a 196 square unit grid of plots 14.times {|x| 14.times {|y| Plot.new(x, y)}} # grab the plot object at (7,13) with newly defined [] method # print it two different ways puts "inspect => #{Plot.community[7, 13].inspect}" puts "puts => #{Plot.community[7, 13]}" # add some characteristics to a plot using the newly defined < method # and a hash puts "After setting some properties:" puts Plot.community[7, 13] < { :name => "Ruffle Gorge", :electricity => "no" } # add electricity puts "After installing electricity:" puts Plot.community[7,13] < {:electricity => "yes"} # add a whole new plot off main grid Plot.new(21,23) < { :name => "Scrooge's Stooge", :water => "no" } puts "New plot:" puts Plot.community[21,23] I'm not suggesting you do something like this ... just to get you thinking is all. A more appropriate way would be to have a Site object that contains Plot objects that know their locations -- like above, but without @@neighbors, and its methods #[x, y] and #used/ placed in the Site class instead. Todd