I have an array [[1, nil, nil], [1, 123, nil]] in which I am calling uniq on to remove duplicates based on the first value. However I want to specifically keep the duplicate that does not have nil for the second value (123 in this case)
my_array.uniq { |arr| arr.first.id }
Might return [[1, nil, nil]] but I want to ensure it returns [[1, 123, nil]]. Is there any way to do this rails style with uniq?
As thinkgruen stated below, I'm not too concerned with the case where there are 3 duplicates, since calling uniq again without the special condition is an option.
[1, 123, nil]again? from what i understood, it will return[[1, 123, nil], [1, 123, nil]]but I think it's important to cover this case in your question as well.uniqwithout the condition.uniqthe elements would have to be in order already, so that the first one is the one you want.