From: Robert Klemme Date: 2012-06-18T15:45:23+09:00 Subject: Re: Overwriting one Ruby array or arrays with another On Wed, May 2, 2012 at 5:27 AM, Craig Law wrote: > Thanks for the reply > > I gave a fairly basic example so that I could explain my needs. Perhaps > a little too basic. I'm also very new to Ruby. > > Going forward the array of arrays may hold information such as ... > > [1, 1, "X", "20120502"] > > ... so that I have a grid that will display an "X" at x,y coordinates > 1,1 and also hold some metadata such as "20120502" which in this case > would be a date. > > Ultimately I will be creating an HTML grid of variable size that will > house an "X" or "O". The date would be metadata that I'm hoping to > display when you hover over a box when viewing the grid/graph in a > browser. > > If I can produce a hash of {[x, y] => xxxxx } which holds the values "X" > and "20120501" that would be OK with me. > > Would the code for such a hash be something like this ... > > {[1, 1] => ["X", "20120501"]} > {[1, 2] => ["O", "20120502"]} > > ... and if so how would I overwrite one hash with the other? Frankly, I'd take a different approach: from what we have seen you have a grid, coordinates and node data. I'd rather model these as separate classes, e.g. https://gist.github.com/2947179 Now you can do g = Grid.new 10, 500 g[Coord(1,3)] = NodeData.new "foo" g.replace( Coord(1,2) => NodeData.new("hello"), Coord(2,34) => nil, ) etc. Then, if you find you need to change the internal representation to be more efficient your algorithms are not affected since they just use the same API of Grid. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/