Ruby Programming
Array Methods
Value Existence
Ruby Tips & Tricks
Coding Tutorial

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.

Practice algorithms

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?.

ruby
1numbers = [1, 2, 3, 4, 5]
2
3puts numbers.include?(3)  # true
4puts numbers.include?(9)  # false

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.

ruby
1numbers = [1, 2, 3, 4, 5]
2
3puts numbers.any? { |n| n > 4 }   # true
4puts numbers.any? { |n| n.even? } # true

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.

ruby
1colors = ["red", "blue", "green"]
2match = colors.find { |color| color.start_with?("bl") }
3
4puts match  # blue

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.

ruby
1letters = ["a", "b", "c"]
2position = letters.index("b")
3
4puts position           # 1
5puts letters.index("z").nil?  # true

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.

ruby
1users = [
2  { id: 1, name: "Alice" },
3  { id: 2, name: "Bob" }
4]
5
6puts users.any? { |user| user[:id] == 2 }  # true

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.

ruby
1require "set"
2
3values = Set.new([1, 2, 3, 4, 5])
4puts values.include?(3)  # true

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'
  • 'find when you need the matching element'
  • 'index when 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 find when you need the matching element itself.
  • Use index when the element's position matters.
  • Switch to Set if you need frequent membership checks on large collections.

Related reading
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.