From: "Brian Schröder" Date: 2005-10-17T07:31:51+09:00 Subject: Re: how to create a hash from 2 arrays? On 17/10/05, Tiggerius wrote: > keys = ["1", "2", "3", "4", "5"] > vals = ["a", "b", "c", "d", "e", "f", "g"] > > I want h = {"1"=>"a", "2"=>"b", "3"=>"c", "4"=>"d", "5"=>"e"} > > right now I do it by: > for i in 0..keys.size-1 > h[keys[i]]=vals[i] > end > > is there a ruby way, more slick way of doing this? Thanks. > > > Not as slick as simons version, but never miss a possibility to use inject. irb(main):001:0> keys = ["1", "2", "3", "4", "5"] => ["1", "2", "3", "4", "5"] irb(main):002:0> vals = ["a", "b", "c", "d", "e", "f", "g"] => ["a", "b", "c", "d", "e", "f", "g"] irb(main):003:0> keys.zip(vals).inject({}) { | h, (k, v) | h[k] = v; h } => {"1"=>"a", "2"=>"b", "3"=>"c", "4"=>"d", "5"=>"e"} regards, Brian -- http://ruby.brian-schroeder.de/ Stringed instrument chords: http://chordlist.brian-schroeder.de/