How to check if a value exists in an array in Ruby
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
In Ruby, the usual way to check whether an array contains a value is array.include?(value). That is the direct built-in answer for ordinary membership checks. Ruby also provides methods such as any?, find, and index when you need something slightly different, such as a condition-based check, the matching element itself, or its position.
So the first question is not just "does it exist." The first question is what kind of existence check you actually need.
The Simplest Answer: include?
If you want to know whether a specific value is present, use include?.
This is the clearest choice when the check is plain value membership.
Ruby uses equality semantics for the comparison, so the behavior depends on how the objects in the array define equality.
Use any? for Condition-Based Checks
Sometimes you are not checking for one exact value. You are checking whether any element satisfies a condition.
This is a better fit than include? when the question is based on a rule rather than direct equality.
Use find When You Need the Element Itself
If you want the matching element, not just true or false, use find or detect.
find returns the first matching element or nil, which makes it useful when you want to continue working with the matched object afterward.
Use index When Position Matters
If you need the position of the element, use index.
This is the right tool when membership is tied to location rather than just presence.
Arrays of Hashes or Objects Need a Predicate
When the array contains hashes or custom objects, include? checks the whole element, not one field inside it.
In this kind of case, any? is usually clearer than include? because the condition focuses on the property you actually care about.
Think About Performance on Large Repeated Checks
All of these array checks are linear in the size of the array because Ruby may need to scan each element.
If you need many repeated membership checks, a Set can be better.
That is worth considering when membership testing is part of a hot path rather than a one-off query.
A Practical Rule of Thumb
Use:
- '
include?for direct value membership' - '
any?for condition-based existence' - '
findwhen you need the matching element' - '
indexwhen you need the position'
That keeps the code readable and makes the intent clear to the next person reading it.
Common Pitfalls
The biggest mistake is using find when all you really need is a boolean. Another is expecting include? to perform a field-level or partial-object comparison automatically when it actually compares the full element. Developers also sometimes keep scanning a large array repeatedly when a Set would be a better structure for frequent membership checks. Finally, if equality semantics on custom objects are not what you expect, include? can behave differently than a field-based predicate.
Summary
- Use
array.include?(value)for ordinary membership checks. - Use
any?when the test depends on a condition. - Use
findwhen you need the matching element itself. - Use
indexwhen the element's position matters. - Switch to
Setif you need frequent membership checks on large collections.
Related reading
- How to check if a variable is a dictionary in Python?
- How to check if a view controller is presented modally or pushed on a navigation stack?
- How to check if AlarmManager already has an alarm set?
- How to check if all elements of a list match a condition?
- How to check if an element is in an array
- How to check if object is an array of a certain type?
- How to check if one of the following items is in a list?
- How to check if PHP array is associative or sequential?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.