From: "David A. Black" Date: 2005-07-23T10:46:11+09:00 Subject: Re: FAQ? Hash concatenation Hi -- On Sat, 23 Jul 2005, William James wrote: > > > Mark J. Reed wrote: >> I've read matz's explanation for the lack of a concatenation operator for >> Hashes in Ruby, but I was wondering if there were a module that supplied one >> anyway. Or at least a self-modifying method that works like update() but >> without clobbering existing values. > > h={'a',1, 'c',3, 'd',4} > h={'a',99, 'b',2}.update(h) > --> {"a"=>1, "b"=>2, "c"=>3, "d"=>4} However.... irb(main):006:0> h={'a',1, 'c',3, 'd',4} => {"a"=>1, "c"=>3, "d"=>4} irb(main):007:0> h.object_id => 537888058 irb(main):008:0> h={'a',99, 'b',2}.update(h) => {"a"=>1, "b"=>2, "c"=>3, "d"=>4} irb(main):009:0> h.object_id => 537863828 So if it matters that it be the same object, this technique won't be appropriate. Also, if you're using something other than a hash literal, for example: h1 = h2.update(h1) you're going to change h2, which might also be undesireable in some cases. David -- David A. Black dblack@wobblini.net