Let's say I have a hash:
{
first: :value1,
second: :value1,
third: :value2
}
During .map I need to eliminate duplicates so it should be either first or second only. Is it possible to have some workaround like:
{
(:first || :second) => :value1,
third: :value2
}
If not, how can I remove key duplicates in a hash based on condition? Is it possible to pass a condition into the .uniq block?
Thanks,
Yes, it is possible to pass a block to the
#uniqmethod.https://ruby-doc.org/3.2.2/Enumerable.html#method-i-uniq
You can apply something like:
or shorter:
Also, another correct solution if you don't need the keys is to simply take the values:
The second recommendation of yours is valid Ruby code, but incorrect, as it removes one key and evaluates to the following: