From: Martin DeMello Date: 2012-05-02T12:41:36+09:00 Subject: Re: Overwriting one Ruby array or arrays with another On Tue, May 1, 2012 at 8:27 PM, Craig Law wrote: > 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? The built-in hash.update method will do this: ruby-1.9.2-p0 > a = {[1, 1] => "x", [1, 2] => "x", [1, 3] => "x", [2, 1] => "x", [2, 3] => "x", [3, 3] => "x"} => {[1, 1]=>"x", [1, 2]=>"x", [1, 3]=>"x", [2, 1]=>"x", [2, 3]=>"x", [3, 3]=>"x"} ruby-1.9.2-p0 > b = {[1, 1] => "o", [2, 2] => "o", [3, 3] => "o"} => {[1, 1]=>"o", [2, 2]=>"o", [3, 3]=>"o"} ruby-1.9.2-p0 > a.update(b) => {[1, 1]=>"o", [1, 2]=>"x", [1, 3]=>"x", [2, 1]=>"x", [2, 3]=>"x", [3, 3]=>"o", [2, 2]=>"o"} ruby-1.9.2-p0 > a => {[1, 1]=>"o", [1, 2]=>"x", [1, 3]=>"x", [2, 1]=>"x", [2, 3]=>"x", [3, 3]=>"o", [2, 2]=>"o"} Note that this will also fill in "o"s in cells that did not have an "x" in them. martin