From: "trans. (T. Onoma)" Date: 2004-12-08T23:13:12+09:00 Subject: Re: Idiom for creating hash from two arrays On Wednesday 08 December 2004 09:02 am, Jonathan Paisley wrote: | On Wed, 08 Dec 2004 22:30:08 +0900, Glenn Parker wrote: | > Jonathan Paisley wrote: | >> Hash.zipnew ... I quite like the similarity to Hash.new, and the | >> inclusion of the already familiar zip idea is also a good thing. | > | > 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. | | That's certainly a possibility. I suppose the disadvantage of this is that | the semantics would then be quite different between one and two arguments: | | 1 argument : specify the default value | 2 arguments : populate the hash with pairings of keys and values | | I think it would be more natural to extend the existing Hash[] method, if | that didn't cause a conflict with the existing usage. The question, then, | is whether you'd ever want to construct a hash with just one entry, whose | key and value are arrays. | | h = Hash[keys,values] | | I suppose if you did want the one-entry-hash, you could always do: | | h = Hash[key=>value] | or even just | h = { key=>value } How do you set the default value after the fact? h = {} h.default=x #? Personally I have never like how Hash.new works. I've always felt that #new for core classes should be able to take there selves: h1 = { :a=>1, :b=>2 } h2 = Hash.new(h1) Default makes more sense as an option: h2 = Hash.new(h1, :default=>0) Although that take away one somewhat useful construction patterns, namely h2 = Hash.new(:a=>1, :b=>2, :default=>0) #would not work man, how cool might it be if Ruby had a secondary argument divider to group sets of parameters. T.