How do I check to see if my array includes an object?

后端 未结 7 1879
刺人心
刺人心 2020-12-04 23:19

I have an array @horses = [] that I fill with some random horses.

How can I check if my @horses array includes a horse that is already incl

7条回答
  •  渐次进展
    2020-12-04 23:50

    Arrays in Ruby don't have exists? method, but they have an include? method as described in the docs. Something like

    unless @suggested_horses.include?(horse)
       @suggested_horses << horse
    end
    

    should work out of box.

提交回复
热议问题