Hi Matz, >Rejected. Proposal Array#to_h(value=nil) does not actually solve the >problem above. If you want to have _this_ to_h, you need to have >a proper usecase. I don't know what you mean the "problem above"... My purpose is to have a convenient way to convert array to hash for *indexing* or *fast searching* purpose. This indeed is a bit ad-hoc, but it is useful in may cases. I deliberately wanted to map array value (not index) to hash key... otherwise I think it is not useful (i.e., map array key => hash key). I think there might be 2 types of RCRs, one is more "scientific", those deep into the design of language. Another is more "engineering", those add a method/feature to a single class for convinience, like Array#nitems. I think for the latter one, the criteria to accept/reject, is if it is useful for "lots of" Ruby programmers..., if that's your criteria, then I think I will wait to see if others want this :D else, if one of your criterion is to make Ruby "pure", all those things should belong to a library not the language itself (or core classes), then pls also let me know, so that I can have a better idea when propose RCR in the future. Thanks, Shannon >From: Yukihiro Matsumoto <matz / ruby-lang.org> >Reply-To: ruby-talk / ruby-lang.org >To: ruby-talk / ruby-lang.org (ruby-talk ML) >Subject: Re: RCR: Array#to_h >Date: Tue, 25 Oct 2005 15:17:00 +0900 > >Hi, > >In message "Re: RCR: Array#to_h" > on Tue, 25 Oct 2005 12:10:18 +0900, "Shannon Fang" ><xrfang / hotmail.com> writes: > >|Abstract >| >|A simple method that construct a hash from an array. Just like Hash#to_a >|return an array from hash. >| >|Problem >| >|Consider this cenario: I programmed a multi-threaded web page downloader. >|One of its input is an array of urls to download. In the program, I wanted >|to use a hash like {'http://ruby-lang.org/index.html' => '200'}, i.e., use >a >|hash to record the return code of the http request. This way I can avoid >|re-download a page, or miss a page. >| >|For the user of this download program, it is much easier to use an Array >|instead of a Hash: >| >|d = WebPageDownloader.new >|d.links = IO.readlines('list.txt') >| >|Hence, in the program we want to do: >| >|@links = @links.to_h >| >|Proposal >| >|Add a method to_h (not to_hash) in the Array class, so that user can: >| >|a = [1, 2, 3, 4, 5] >|p a.to_h { |i, v| v * 2} --> {5=>10, 1=>2, 2=>4, 3=>6, 4=>8} >|p a.to_h(3) --> {5=>3, 1=>3, 2=>3, 3=>3, 4=>3} > >Rejected. Proposal Array#to_h(value=nil) does not actually solve the >problem above. If you want to have _this_ to_h, you need to have >a proper usecase. > > matz. >