From: Greg Hauptmann Date: 2008-10-30T14:06:50+09:00 Subject: Re: Hash.merge_add! extension - how does this code look? thanks for the great feedback guys - will take all this on board On Tue, Oct 28, 2008 at 10:19 PM, Brian Candler wrote: >> self[k] = Array[self[k]] + Array[v] > > Oops, I meant to write: > > self[k] = Array(self[k]) + Array(v) > > Otherwise, if the value is already an array, it will be wrapped in > another one. > > This does highlight a limitation of this API thought. What if the values > themselves are arrays? > > h = { "foo" => [1,2] } > h.merge_add!({ "foo" => [3,4] }) > # do you want h = { "foo" => [[1,2], [3,4]] } ? > > In that case, it would be better to insist that the hash values are > arrays in the first place, as shown by Stefan Rusterholz's collector > example earlier. > > Also, are you happy for duplicate values to be inserted? > > h = { "foo" => "bar" } > h.merge_add!({ "foo" => "bar" }) > # h = { "foo" => ["bar", "bar"] } ? > -- > Posted via http://www.ruby-forum.com/. > >