mardi 7 avril 2015

In Ruby, is there an Array method that combines 'uniq' and 'compact'?

I am trying to find unique elements in an array and remove the nil values from it. Now my solution looks like this:



> @array = [1,2,1,1,2,3,4,nil,5,nil,5]
=> [1, 2, 1, 1, 2, 3, 4, nil, 5, nil, 5]
> @array.uniq.compact
=> [1, 2, 3, 4, 5]


Is there any single method that does both the operations?


If not, which is efficient? @array.uniq.compact or @array.compact.uniq


Aucun commentaire:

Enregistrer un commentaire