🎉 My new book SaaS Demos That Sell is now available for purchase! 🎉

Merging Two Arrays Into A Hash

Want to merge two arrays together to form a hash? This one was a bit befuddling at first, but after some researchI found a great solution in the Ruby newsgroup. Here’s an example:

teams = ["Pittsburgh Steelers","New England Patriots","Indianapolis Colts"]
qbs = ["Ben Rothlisberger", "Tom Brady", "Peyton Manning"]
team_qbs = {}
teams.zip(qbs) {|a,b| team_qbs[a] = b }

{"Indianapolis Colts"=>"Peyton Manning", "New England Patriots"=>"Tom Brady", "Pittsburgh Steelers"=>"Ben Rothlisberger"}