How to check if a value exists in an array in Ruby

user211662 picture user211662 · Dec 31, 2009 · Viewed 918.9k times · Source

I have a value 'Dog' and an array ['Cat', 'Dog', 'Bird'].

How do I check if it exists in the array without looping through it? Is there a simple way of checking if the value exists, nothing more?

Answer

Brian Campbell picture Brian Campbell · Dec 31, 2009

You're looking for include?:

>> ['Cat', 'Dog', 'Bird'].include? 'Dog'
=> true