Never been to DZone Snippets before?

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 (See related posts)

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 on this post

bilbo8000 posts on Aug 02, 2005 at 15:40
no comment
seancribbs posts on Apr 26, 2007 at 10:52
How about this? (assume the arrays are of the same size)
Hash[*keys.zip(values).flatten]


The splat can be your friend!

You need to create an account or log in to post comments to this site.


Click here to browse all 4862 code snippets

Related Posts