From: Shannon Fang Date: 2005-10-25T13:38:06+09:00 Subject: Re: RCR: Array#to_h The block may be useful because you may want to, for example, do some calculation, or even lookup the database... Shannon >From: "Marcel Molina Jr." >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:16:04 +0900 > >On Tue, Oct 25, 2005 at 12:10:18PM +0900, Shannon Fang wrote: > > Abstract > > > > A simple method that construct a hash from an array. Just like Hash#to_a > > return an array from hash. > >Having your proposed to_h accept a block is a nice idea but as it is you >can >do the non-block version quite easily: > > >> Hash[*%w(a b c d)] >=> {"a"=>"b", "c"=>"d"} > >> keys = %w(a b c d) >=> ["a", "b", "c", "d"] > >> values = [1, 2, 3, 4] >=> [1, 2, 3, 4] > >> Hash[*keys.zip(values).flatten] >=> {"a"=>1, "b"=>2, "c"=>3, "d"=>4} > >marcel >-- >Marcel Molina Jr. >