Add element to an array if it's not there already

后端 未结 6 502
逝去的感伤
逝去的感伤 2021-01-30 12:02

I have a Ruby class

class MyClass
  attr_writer :item1, :item2
end

my_array = get_array_of_my_class() #my_array is an array of MyClass
unique_array_of_item1 = [         


        
6条回答
  •  孤独总比滥情好
    2021-01-30 12:42

    @Coorasse has a good answer, though it should be:

    my_array | [item]
    

    And to update my_array in place:

    my_array |= [item]
    

提交回复
热议问题