Here's a quick and dirty way of getting the union:
(array1 + array2).uniq{|a| a[:name]}
However, I would recommend creating your own subclass of Hash
so you can safely override eql?
as Cary Swoveland points out is what the set-like operators rely on. Note that you also need to limit the hash
method to only provide the hashing function on the name field.
class Guy < Hash
def eql?(other_hash)
self[:name] == other_hash[:name]
end
def hash
self[:name].hash
end
end
Then these Guy
objects will work in all of the set operations:
array1 = [ Guy[name:'Guy1', age: 45], Guy[name:'Guy2', age: 45] ]
array2 = [ Guy[name:'Guy1', age: 46], Guy[name:'Guy3', age: 45] ]
array1 - array2
#=> [{:name=>"Guy2", :age=>45}]
array2 - array1
#=> [{:name=>"Guy3", :age=>45}]
array1 | array2
#=> [{:name=>"Guy1", :age=>45}, {:name=>"Guy2", :age=>45}, {:name=>"Guy3", :age=>
45}]
array1 & array2
#=> [{:name=>"Guy1", :age=>45}]