HiveBrain v1.2.0
Get Started
← Back to all entries
snippetrubyCritical

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

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
arrayhowcheckexistsrubyvalue

Problem

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?

Solution

You're looking for include?:

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

Code Snippets

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

Context

Stack Overflow Q#1986386, score: 2248

Revisions (0)

No revisions yet.