No, but you can add them in any order you like.
array.uniq.compact array.compact.uniq
As pointed out in phts, you can pass the uniq block, but I don't see that this is a very useful alternative for uniq.compact .
However, for better speed, the following may help:
[].tap do |new_array| hash = {} original_array.each do |element| next if element.nil? || !hash[element].nil? new_array << (hash[element] = element) end end
Finally, if speed is not an issue, and you will often use this method, then you can create your own method:
class Array def compact_uniq self.compact.uniq end def compact_blank
Humza
source share