I have 2 arrays of objects, I want to merge them such that the resulting array will contain all elements in the first array replacing any elements for which the second array has an object of the same id.
finalArr=[]
arr1.each do |e1|
set2Contains=false
arr2.each do |e2|
if(e2.id==e1.id)
set2Contains=true
end
end
if(set2Contains)
finalArr.push(e2)
else
finalArr.push(e1)
end
end
I'm new to ruby, but as it is the king of the one liners the above seems a little verbose. I was wondering if my code could be shortened / optimized in any way?
Thanks for any suggestions