I have an array of structs and I need to keep them unique based on an attribute, :num
. This array will be appended with new struct objects. If the new struct object has a duplicate :num
attribute value, I want to remove the old element containing duplicate :num
and replace it with the new one.
Here's what I mean. Let's set it up:
Gen = Struct.new(:num, :val)
arr = []
arr << Gen.new(11, "foo1")
arr << Gen.new(12, "foo2")
# [#<struct num=11, val="foo1">, #<struct num=12, val="foo2">]
Then I feed it with new structs. Let's construct them:
s1 = Gen.new(12, "foo10")
s2 = Gen.new(13, "foo3")
- If I do
arr << s1
, it will just appends1
intoarr
. - If I do
arr.uniq {|el| el.number}
after I did the above, it sometimes remove "foo10", but I want to always keep the latest struct.
When s1
is appended into arr
, it needs to replace the old #<struct num=12, val="foo2">
struct because s1
is the newest feed. Since arr
already contains :num
12, we need to replace it with the new struct. Since s2
contains a unique :num
, it should be able to be appended no problem (even should s2
contain a duplicate :val
, it should not matter, I am only concerned with keeping :num
unique).
In the end, I need arr
to look like:
[#<struct num=11, val="foo1">, #<struct num=12, val="foo10">, #<struct num=13, val="foo3">]
The order of the array doesn't matter.
I've looked at Add element to an array if it's not there already, Ruby condition for inserting unique items into an array, Remove duplicate elements from array in Ruby , and a few other posts. They are all mostly dealing with simple array, not array of structs.
How can I achieve this?