From: Brian Candler Date: 2003-07-21T23:59:50+09:00 Subject: Re: Proposal: Array#to_h, to simplify hash generation On Mon, Jul 21, 2003 at 11:34:39PM +0900, Gavin Sinclair wrote: > > I'm not sure how this differs from (rejected) RCR#12 (except for > > having to return a key as well as a value). > > How did O.J. Simpson's second trial differ from his first? ;) > > Anyway, I think returning a key as well as a value is a significant > difference: > - much more flexible (I create all kinds of hashes all the time in > my code, and could really use that flexibility) > - less magical, more scrutible: having two values makes it clear what > is going on, given that we're dealing with a hash. With the > single-value to_hash/hashify, I had to keep reminding myself what > it meant; not so with the new "make_hash". Can I suggest another name - "collect_hash" - since that's basically what it is? collect {... return [x,y] } =>> [[a,b], [c,d], ...] collect_hash {... return [x,y] } =>> {a=>b, c=>d, ...} In which case it clearly belongs in Enumerable - see Gavin's implementation in [RubyTalk:76446] It's still not an inverse operation to Hash#to_a, and I think there could still be value in that. You could simulate it of course, using myhash = myarray.collect_hash { |pair| pair } If we didn't have Hash#to_a then it could also be implemented as myarray = myhash.collect { |pair| pair } But we do, so we don't bother. Regards, Brian.