From: Simon Strandgaard <0bz63fz3m1qt3001@...> Date: 2003-07-22T05:46:35+09:00 Subject: Re: Proposal: Array#to_h, to simplify hash generation On Tue, 22 Jul 2003 06:00:53 +0900, Brian Candler wrote: > On Tue, Jul 22, 2003 at 02:24:35AM +0900, Simon Strandgaard wrote: >> On Tue, 22 Jul 2003 02:57:40 +0900, Brian Candler wrote: >> >> > On Tue, Jul 22, 2003 at 01:43:46AM +0900, Brian Candler wrote: >> >> The bit I don't like is Hash#<< taking an array of two elements, rather than >> >> having two arguments >> > >> > I forgot about using Method#arity. New version attached... Brian. >> > >> > class Array # ditto for Enumerable >> > def collect(dest=Array,*args) >> > res = dest.new(*args) >> >> Maybe its better to do the #new outside collect ? > > Yes, I worked that out in parallel with you :-) Thinking while walking home, > the pattern :-) > Since not all objects necessarily have a '<<' method then maybe it's worth > having that as a parameter as well. The attached code lets you do that, or > guesses at a suitable method if one isn't provided. Good idea to test for different append methods [snip code] #collect_into / #map_into is nice. How about abbreviating the name, so its just: #into p [1, "2", 3].into({}) { |i| [i.class, i] } # {"Fixnum"=>[1, 3], "String"=>["2"]} Important: Hash#append should not overwrite earlier entries. -- Simon Strandgaard