user1771791
user1771791

Reputation: 5

Loop through array and delete all duplicates in ruby

I need to loop through an array called @ads and delete all objects in it that are duplicates of one another. how do i do this?

Upvotes: 0

Views: 389

Answers (2)

xdazz
xdazz

Reputation: 160843

The below code will give you the result uniqic by id.

@ads = @ads.uniq{|ad| ad.id}

Upvotes: 4

Castilho
Castilho

Reputation: 3187

@ads = @ads.uniq

or

@ads.uniq!

Upvotes: 4

Related Questions