Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending a collect array with unique values

So I have an array built by collect.

@a = Relation.where(part: "v04")

@relations = @a.collect {|x| x.car}

Builds..

=> ["f03", "f04"]

@a = Relation.where(part: "v03")

@relations = @a.collect {|x| x.car}

Builds..

=> ["f01", "f03"]

What I want is to append the collect so that I can build an array from both v03 and v04 so that it looks like this.

=> ["f03", "f04", "f01", "f03"]

And then only keeps unique values so that it looks like this.

=> ["f03", "f04", "f01"]

Take out f03 since it was listed twice.

like image 377
San Backups Avatar asked Sep 01 '12 19:09

San Backups


1 Answers

["f03", "f04"] | ["f01", "f03"] #=> ["f03", "f04", "f01"]

car1 = ["f03", "f04"]
car2 = ["f01", "f03"]

car1 | car2 #=> ["f03", "f04", "f01"]
like image 152
megas Avatar answered Feb 12 '23 17:02

megas