From: Robert Klemme Date: 2012-11-07T07:37:11+09:00 Subject: Re: How to merge two or more hashes in to one? --047d7b2e089f12b89804cddb3e48 Content-Type: text/plain; charset=ISO-8859-1 On Tue, Nov 6, 2012 at 5:29 PM, Jermaine O. wrote: > > The same thing can be achieved with Enumerable#inject, which is the > > generalization of this kind of "sum up the elements of a collection": > > Just what I need. Many thanks for this, works great. Appreciate it! > I'd rather use Set here since apparently entries must be present only once: require 'set' merged = Hash.new {|h,k| h[k] = Set.new} hashes.each do |hash| hash.each {|k, v| merged[k].merge(v)} end Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/ --047d7b2e089f12b89804cddb3e48 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

On Tue, Nov 6, 20= 12 at 5:29 PM, Jermaine O. <lists@ruby-forum.com> wrote:<= br>
> The same thing can be achieved with Enumerable#injec= t, which is the
> generalization of this kind of "sum up the elements of a collecti= on":

Just what I need. Many thanks for this, works great. Appreciate it!

I'd rather use Set here since apparently entrie= s must be present only once:

require 'set'

merged =3D= Hash.new {|h,k| h[k] =3D Set.new}

hashes.each do |hash|
=A0 hash.each {|k, v| merged[k].merge(v)}
e= nd

Cheers

robert

--
rem= ember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
--047d7b2e089f12b89804cddb3e48--