如何检查一个值是否存在于 Ruby 中的数组中

问题描述:

我有一个值 'Dog' 和一个数组 ['Cat', 'Dog', 'Bird'].

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?

您正在寻找 include?:

You're looking for include?:

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