From: Shannon Fang Date: 2005-10-25T13:46:33+09:00 Subject: Re: RCR: Array#to_h Ok, we may consider Array#to_h as a true reverse operation of Hash#to_a, however, I think the feature I proposed has different usage. May be anyone has a better name for easy understanding? Also, I am not very clear about this code: > module Enumerable > def to_h > hash = {} > each {|key, value| hash[key] = value} > hash > end > end > Array mixed Eumerable, hence, if use the above code, we have: arr = [a, b, c, d, e] arr.to_h => {1 => a, 2 => b, 3 => c, 4 => d, 5 =>e} Which seems not very useful, I think the to_h operation of array should use array VALUE as the hash KEY. Shannon >From: nobuyoshi nakada >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 12:48:40 +0900 > >Hi, > >At Tue, 25 Oct 2005 12:10:18 +0900, >Shannon Fang wrote in [ruby-talk:162399]: > > Abstract > > > > A simple method that construct a hash from an array. Just like Hash#to_a > > return an array from hash. > >Agreed here, but > > > 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} > >I expect that method as: > > h = {"foo"=>1,"bar"=>2} > a = h.to_a # => [["foo", 1], ["bar", 2]] > a.to_h # => {"foo"=>1,"bar"=>2} == h > > module Enumerable > def to_h > hash = {} > each {|key, value| hash[key] = value} > hash > end > end > >-- >Nobu Nakada >