Beginners Ruby Method Guide (8 Part Series)
Okay, you want to remove duplicates from your array. As a beginner this might prove a difficult task. Luckily, Ruby has created more than one way to do this, so you don't spend your time writing a function for it. Let's get into it.
So, what are these methods anyway? Well, since you asked so nicely you can use the unique method (.uniq), the difference operator(-), the union operator(|), and the intersection operator(&), your own short-hand method(which we'll get into later), and to_set.
Let's take a look at the syntax for each method.
These three operators have different uses. The Difference operator returns items from the first array that DON'T appear in the second and vice versa. The Union operator can be used to return one of each item in both arrays, which guarantees that there will not be any duplicates. The Intersection operator returns items that appear in both arrays, also without duplicates.
After all I've said you're probably thinking,
but to your surprise there actually is a reason that I'm telling you this. Here, let me show you.
Sets are unordered lists that can't have duplicates. So the to_set method creates an instance of the array to a set. Making all the duplicates disappear. So you can assign the variable to the duplicate free list.
Mine is not as good, for it to work it has to return the array sorted. Anyway, all this reading is probably boring you so I hope you liked this post, if you have any questions put them in the comments.