From: "shortcutter@..." Date: 2008-05-28T23:05:32+09:00 Subject: Re: Merge collections of objects On 28 Mai, 15:42, Alex Fenton wrote: > John Butler wrote: > > Hi, > > > I wonder if there is a quick way to do this. > > > companyA has 2 employees "a", "b" > > > companyB has 3 employees "a", "b", "c" > > > I basically want to merge companyA employess with companyB employees so > > when i look at companyA the employees will equal "a", "b", "c" > > Try the "|" method of Array > > irb(main):001:0> comp_a = %w[a b c] > => ["a", "b", "c"] > irb(main):002:0> comp_b = %w[a b] > => ["a", "b"] > irb(main):003:0> comp_a | comp_b > => ["a", "b", "c"] > irb(main):004:0> comp_a & comp_b > => ["a", "b"] > > a For efficient processing of large collections there's also Set and Hash. irb(main):001:0> a=%w{a b c} => ["a", "b", "c"] irb(main):002:0> b=%w{b c d} => ["b", "c", "d"] irb(main):003:0> a.to_set | b.to_set => # irb(main):004:0> h=Hash.new 0 => {} irb(main):005:0> [a,b].each {|c| c.each {|x| h[x]+=1}} => [["a", "b", "c"], ["b", "c", "d"]] irb(main):006:0> h.keys => ["a", "b", "c", "d"] irb(main):007:0> etc. Kind regards robert