Forum: Ruby matching arrays within arrays

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cd58ba31277139562fa17cd36c5fc789?d=identicon&s=25 Nick Black (Guest)
on 2007-01-23 16:20
(Received via mailing list)
Hello,

I have a number of arrays within arrays.  Some of them are duplicatess
like:

[[foo, bar, ru, by], [ru], [bar, foo], [foo, bar, ru, by]]

I would like to remove duplicates to end up with:

[[foo, bar, ru, by], [ru], [bar, foo]]

Is there a Ruby function to do this?


Cheers,
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (regularfry)
on 2007-01-23 16:24
(Received via mailing list)
Nick Black wrote:
>
> Is there a Ruby function to do this?

irb(main):007:0> [['foo', 'bar', 'ru', 'by'], ['ru'], ['bar', 'foo'],
['foo', 'bar', 'ru', 'by']].uniq
=> [["foo", "bar", "ru", "by"], ["ru"], ["bar", "foo"]]
De271a04fe7a67b884ce75404c1dcc61?d=identicon&s=25 Chris Gernon (kabigon)
on 2007-01-23 16:25
Nick Black wrote:
> Is there a Ruby function to do this?

uniq will work. It doesn't matter what type of object the members of the
array are, so it can be used for an array of arrays.

irb> [['foo', 'bar', 'ru', 'by'], ['ru'], ['bar', 'foo'], ['foo', 'bar',
'ru', 'by']].uniq
=> [["foo", "bar", "ru", "by"], ["ru"], ["bar", "foo"]]
Cd58ba31277139562fa17cd36c5fc789?d=identicon&s=25 Nick Black (Guest)
on 2007-01-23 16:40
(Received via mailing list)
Cheers!
This topic is locked and can not be replied to.