DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Zip Two Arrays Together Into A Hash
Found this here:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/122906
def Hash.from_pairs_e(keys,values)
hash = {}
keys.size.times { |i| hash[ keys[i] ] = values[i] }
hash
end 





Comments
Snippets Manager replied on Tue, 2010/10/12 - 8:22am
Hash[*keys.zip(values).flatten]Consider what happens if some of the keys or values are arrays.Snippets Manager replied on Thu, 2012/03/29 - 8:48am
Hash[[1,2,3].zip [9,8,7]] => {1=>9, 2=>8, 3=>7}and no monkey codeSnippets Manager replied on Mon, 2008/05/26 - 3:24pm
class Hash # # Create a hash from an array of keys and corresponding values. def self.zip(keys, values, default=nil, &block) hsh = block_given? ? Hash.new(&block) : Hash.new(default) keys.zip(values) { |k,v| hsh[k]=v } hsh end endEx.hsh = Hash.zip([:foo, :bar, :baz], [1,2]) # => {:foo=>1, :baz=>nil, :bar=>2}Snippets Manager replied on Mon, 2008/05/26 - 3:24pm
Snippets Manager replied on Wed, 2006/03/29 - 10:25pm
Hash[*keys.zip(values).flatten]The splat can be your friend!Snippets Manager replied on Mon, 2012/05/07 - 2:12pm