From: Yukihiro Matsumoto Date: 2005-10-25T15:17:00+09:00 Subject: Re: RCR: Array#to_h Hi, In message "Re: RCR: Array#to_h" on Tue, 25 Oct 2005 12:10:18 +0900, "Shannon Fang" 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.