Page 1 of 2

Ruby question - Filtering array duplications

PostPosted: Thu May 16, 2019 11:29 am
by kortezzzz
Hi,

I tried few methods for this from the Ruby guide, but couldn't translate them with correct syntax in FS.

The example:
We have a given array:
dog
dog
frog
fish
cat
cat

We need to filter the duplications which are "dog" and "cat" and end up with
dog
frog
fish
cat

What would be the correct syntax for that?

Re: Ruby question - Filtering array duplications

PostPosted: Thu May 16, 2019 12:11 pm
by DaveyBoy
Try array.uniq :)

Re: Ruby question - Filtering array duplications

PostPosted: Thu May 16, 2019 12:21 pm
by kortezzzz
Thanks Dave. I tried it but couldn't formulate it correctly. What exact syntax would you use to make it work in FS?

Re: Ruby question - Filtering array duplications

PostPosted: Thu May 16, 2019 1:30 pm
by DaveyBoy
Like so:

["dog","dog","frog","fish","cat","cat"].uniq

Here's what I use for reference:
https://ruby-doc.org/core-1.9.3/Array.html#method-i-uniq

Hope this helps :)

Re: Ruby question - Filtering array duplications

PostPosted: Thu May 16, 2019 2:51 pm
by kortezzzz
Thanks,

But what if the array is not pre-determined (to "dog", cat", etc.)? Is there any kind of code that can recognize duplications in any given array?

Re: Ruby question - Filtering array duplications

PostPosted: Thu May 16, 2019 4:18 pm
by DaveyBoy
Hmmm . . .I don't really understand what you're asking, uniq will remove any and all dupes as far as I know.

Can you be more specific in what you are trying to achieve?

Re: Ruby question - Filtering array duplications

PostPosted: Thu May 16, 2019 8:15 pm
by tulamide
DaveyBoy is right. The solution to what you described is Array #uniq, and it doesn't matter, what exactly is in the arrays, as long as the objects are comparable.

Re: Ruby question - Filtering array duplications

PostPosted: Thu May 16, 2019 10:38 pm
by kortezzzz
Ok, so there is a method for this :)
But how I write it correctly inside FS Ruby module? Tried to formulate a code, but it didn't worked for me, so seems like I'm doing something wrong.

Re: Ruby question - Filtering array duplications

PostPosted: Fri May 17, 2019 1:29 am
by RJHollins
kortezzzz wrote:Ok, so there is a method for this :)
But how I write it correctly inside FS Ruby module? Tried to formulate a code, but it didn't worked for me, so seems like I'm doing something wrong.

Post us an example of the code, so we can see what you are trying to do.

Re: Ruby question - Filtering array duplications

PostPosted: Fri May 17, 2019 10:42 pm
by kortezzzz
Found how to formulate it correctrlly in FS. An example schematic is attached.

Cheers!