From: "Jan E." Date: 2012-11-07T01:14:06+09:00 Subject: Re: How to merge two or more hashes in to one? Jermaine O. wrote in post #1083159: > Hi Jan, I appreciate your help but care to give an example? I suppose you have an array of hashes. Define a variable for the intermediate results and initialize it with an empty hash. Then you loop through the hashes and merge each one with the intermediate hash: #------------------------- data = [ {luxury: ["Mercedes", "BMW"], sport: ["Ferrari"]}, {luxury: ["BMW", "Bentley"], sport: ["Lamborghini"]}, {luxury: ["something luxury"], sport: ["something sporty"]}, ] merged = {} data.each do |cars| merged.merge!(cars) {|_, v_1, v_2| v_1 | v_2} end p merged #------------------------- The same thing can be achieved with Enumerable#inject, which is the generalization of this kind of "sum up the elements of a collection": #------------------------- merged = data.inject do |cars_1, cars_2| cars_1.merge(cars_2) {|_, v_1, v_2| v_1 | v_2} end p merged #------------------------- -- Posted via http://www.ruby-forum.com/.