From: Glenn Parker Date: 2004-12-08T23:25:10+09:00 Subject: Re: Idiom for creating hash from two arrays David A. Black wrote: > On Wed, 8 Dec 2004, Glenn Parker wrote: >> Why does it need a new name? Instead, why not extend Hash.new to do >> this when it is passed two arrays? It seems pretty natural to me, and >> I don't see any conflict with existing usage. > > What if you wanted to set a default value for the hash? Hash.new is already varadic: Hash.new => hash Hash.new(obj) => aHash Hash.new {|hash, key| block } => aHash The second form specifies a default value. The third form specifies a block to generate missing values. I was suggesting that we add one more form: Hash.new(keyArray, valueArray) => aHash This form is easily distinguishable from the original three, so you could still create a hash with a default value using the second or third form. However, the more I think about it, the more I see this as a typical operation on existing hashes, not simply a constructor. So, I would rather see alternate forms for merge and update: Hash#merge(keyArray, valueArray) Hash#update(keyArray, valueArray) Then, to the original problem would be expressed as: h = Hash.new.merge(keyArray, valueArray) -- Glenn Parker | glenn.parker-AT-comcast.net |