Ruby example

How to check if an array contains a value in Ruby

8 min read Updated Dec 2026 Runs in an isolated runtime
Quick answer

Use include?: fruits.include?("banana") returns true or false (member? is the same method). To match on a condition, such as a field in an array of hashes, pass a block to any?: users.any? { |u| u[:name] == "Bob" }. Use find when you want the matching element back and index when you want its position.

Ruby has no in operator for membership and no contains method: the check is include?, and the Enumerable methods with a block cover everything else. The sections below cover a plain value with include?, a condition or an array of hashes with any?, getting the first match or its index with find and index, checking for any of several values with intersect?, why any? is the wrong way to ask whether an array is empty, and a Set or a binary search for many lookups, then a table to pick from. Each example runs on this page: hit Run, then edit the code and run it again.

1include? and member?Recommended

array.include?(value) walks the array and returns true as soon as an element is == to the value, false otherwise. member? is another name for it; most Ruby code uses include?. Because it compares with ==, the usual equality rules apply: 1 equals 1.0, a string never equals a symbol, strings compare case-sensitively, and nested arrays compare by content. Rails adds the reverse form value.in?(array) (and exclude?) through ActiveSupport; plain Ruby has neither.

include.rb
fruits = ["apple", "banana", "cherry"]

puts "banana? #{fruits.include?("banana")}"
puts "grape?  #{fruits.include?("grape")}"
puts "member? #{fruits.member?("cherry")}"

# The usual if / unless
puts "We have apples" if fruits.include?("apple")
puts "No grapes" unless fruits.include?("grape")

# include? compares with ==
p [1, 2, 3].include?(2.0)
p ["a", "b"].include?(:a)
p ["Apple"].include?("apple")
p [[1, 2], [3, 4]].include?([3, 4])
p [nil, 0].include?(nil)

# Coming from Python? "x in list" is pattern matching in Ruby
if 2 in [1, 2]
  puts "2 in [1, 2]: matched"
else
  puts "2 in [1, 2]: no match"
end

Output

The five p lines print true, false, false, true, true: 2.0 is found because 2 == 2.0, the symbol :a is not the string "a", "Apple" is not "apple", and nil is a value like any other. The last line is the trap for Python readers: if 2 in [1, 2] is valid Ruby but asks whether 2 matches the pattern [1, 2] (an array of exactly those two elements), so it prints no match without any error.

2A condition or an array of hashes: any? with a block

When the test is more than equality, pass a block to any?. It calls the block for each element and stops at the first one that returns a truthy value, so it does no more work than include?. This is the answer for an array of hashes (or of objects): compare the field you care about inside the block. You can also pull the field out with map and use include?, or, with Ruby 3 pattern matching, check several keys at once with u in { role: "dev", age: 41 }, which ignores the hash's other keys.

array_of_hashes.rb
users = [
  { name: "Ann", role: "admin", age: 31 },
  { name: "Bob", role: "dev", age: 41 },
  { name: "Cy", role: "dev", age: 25 }
]

# Is there a user with this name?
puts "Bob: #{users.any? { |u| u[:name] == "Bob" }}"
puts "Dan: #{users.any? { |u| u[:name] == "Dan" }}"

# Ruby 3.4+: it is the block's implicit parameter
puts "admin: #{users.any? { it[:role] == "admin" }}"

# Pull one field out, then include?
names = users.map { |u| u[:name] }
p names
p names.include?("Cy")

# Pattern matching: a hash with these keys and values
p users.any? { |u| u in { role: "dev", age: 41 } }
p users.any? { |u| u in { role: "dev", age: 99 } }

# Case-insensitive check in a plain array of strings
p names.any? { |n| n.casecmp?("BOB") }

Output

Bob: true and Dan: false come from the same one-line block. The map version builds a whole new array (["Ann", "Bob", "Cy"]) before searching it, so prefer any? when you only need a yes or no. The pattern lines print true for Bob's role and age and false when the age differs. Since include? is case-sensitive, a case-insensitive search needs a block too: casecmp? finds "Bob" when you ask for "BOB". The it parameter is Ruby 3.4 and later; on older versions name it, as in |u|.

3Get the match or its position: find, index and select

Often "is it there?" is followed by "give it to me". Instead of any? and a second search, call find (also named detect): it returns the first element the block accepts, or nil when none does. index (also named find_index) returns the position instead, takes either a value or a block, and rindex searches from the end. select (also named filter) returns every match as an array, empty when there are none.

find_first.rb
User = Struct.new(:name, :age)

users = [User.new("Ann", 31), User.new("Bob", 41), User.new("Cy", 25)]

# find (alias detect) returns the first match, or nil
p users.find { |u| u.age > 30 }
p users.detect { |u| u.name == "Cy" }
p users.find { |u| u.age > 50 }

# &. reads a field only when something was found
p users.find { |u| u.name == "Dan" }&.age

# index (alias find_index) gives the position instead
p users.index { |u| u.name == "Bob" }
p users.find_index { |u| u.name == "Dan" }

letters = %w[a b c b]
p letters.index("b")
p letters.rindex("b")

# select (alias filter) returns every match
p users.select { |u| u.age > 30 }.map(&:name)
p users.select { |u| u.age > 50 }

# find can't tell "found nil" from "found nothing"
values = [nil, 1]
p values.find(&:nil?)
p values.any?(&:nil?)
p values.index(nil)

Output

find stops at Ann, the first user over 30, even though Bob matches too; use select to get both (["Ann", "Bob"]). A miss gives nil from find and index and [] from select, and &. turns a missing user's age into nil instead of a NoMethodError. The last three lines show the one case where find misleads: when the element you are looking for is nil, it returns nil whether it found it or not. Ask any? or index instead, which report true and 0.

4Any, all or none of several values

To check whether an array contains any value from another array, use intersect?, added to Array in Ruby 3.1. On older versions, take the intersection with & and ask whether it has elements, or loop over the wanted values with any?. For "all of them", subtract and check for an empty result; for "none of them", use none?. count answers how many times a value (or a block's condition) appears.

any_of_several.rb
tags = ["ruby", "rails", "web"]
wanted = ["python", "ruby"]

# Does tags contain any of the wanted values?
p tags.intersect?(wanted)
p tags.intersect?(["go", "rust"])

# The same check before Ruby 3.1
p((tags & wanted).any?)
p wanted.any? { |w| tags.include?(w) }

# Which ones are there?
p tags & wanted

# All of them? None of them?
p (["ruby", "web"] - tags).empty?
p ["ruby", "web"].all? { |w| tags.include?(w) }
p ["go", "rust"].none? { |w| tags.include?(w) }

# How many times does a value appear?
votes = ["yes", "no", "yes", "yes"]
p votes.count("yes")
p votes.count { |v| v != "yes" }

Output

intersect? prints true because "ruby" is in both arrays, and tags & wanted shows which values matched: ["ruby"]. intersect? stops at the first shared value instead of building that array, so prefer it when you only need a yes or no. Both "all" checks print true: the subtraction leaves nothing, and all? finds every word. count("yes") is 3; count with no argument and no block is just the length.

5any? is not the opposite of empty?

any? without a block does not ask whether the array has elements; it asks whether any element is truthy. In Ruby only nil and false are falsy, so an array holding only those is "not any?" but also not empty. To ask whether an array has elements, use empty? (or size). Python readers: 0 and "" are truthy in Ruby. With an argument, any?, all? and none? compare with ===, which lets you check for a class, a range or a regex, something include? can't do.

any_vs_empty.rb
# any? without a block asks: is any element truthy?
p [1, 2].any?
p [].any?
p [nil].any?
p [nil, false].any?
p [0, ""].any?

# To ask "does the array have elements?", use empty?
p [nil].empty?
p [].empty?

list = [nil]
puts "has items" unless list.empty?
puts "any? says no" unless list.any?

# any? with an argument compares with ===, include? with ==
p [1, "a", :b].any?(String)
p [1, "a", :b].include?(String)
p [3, 8, 12].any?(5..9)
p ["cat", "dog"].any?(/^d/)

Output

[nil].any? and [nil, false].any? both print false, yet [nil].empty? is also false: the list has an element, which is why the script prints both has items and any? says no. [0, ""].any? is true. In the last block, any?(String) is true because String === "a" tests the class, while include?(String) looks for the String class itself as an element and prints false. The range finds 8 and the regex finds "dog". Rails' present? and blank? are ActiveSupport methods; plain Ruby uses empty?.

6Many lookups: Set, or bsearch on a sorted array

include? on an array compares element by element, so its cost grows with the array's length, and checking many values against a large array repeats that walk every time. A Set stores its elements in a hash table, so Set#include? takes about the same time whatever the size. Build the set once with to_set or Set[…], then look up as often as you like. Since Ruby 3.2, Set is available without require 'set'; the line is harmless and keeps older versions working. If the array is already sorted, bsearch does a binary search without building anything.

set_lookup.rb
require 'set'

allowed = Set["alice", "bob", "carol"]
p allowed.include?("bob")
p allowed.include?("dave")
p allowed

# Build the set once, then look up many times
banned = [17, 42, 99, 256].to_set
requests = [5, 42, 7, 99]
p requests.select { |id| banned.include?(id) }
p requests.reject { |id| banned.include?(id) }

# A sorted array can use bsearch (binary search)
def sorted_include?(sorted, value)
  sorted.bsearch { |x| x >= value } == value
end

sorted = [3, 8, 15, 23, 42, 57]
p sorted.bsearch { |x| x >= 24 }
p sorted_include?(sorted, 23)
p sorted_include?(sorted, 24)

Output

The set answers true for "bob" and false for "dave", and prints as Set["alice", "bob", "carol"]. Filtering the requests against it keeps [42, 99]. bsearch in this form returns the first element for which the block is true, not a yes or no: asking for 24 returns 42, the next larger value, which is why sorted_include? compares the result with the value and prints true for 23 and false for 24. On an unsorted array bsearch gives wrong answers without an error. A set compares with eql? rather than ==, so Set[1, 2].include?(1.0) is false where the array version says true; intersect? and & work the same way. For a handful of elements or a single lookup, plain include? is fine.

7Which should you use?

MethodReturnsCompares withBest for
arr.include?(x)true / false==One plain value: most checks
arr.any? { |e| … }true / falseYour blockArrays of hashes or objects, any condition
arr.find { |e| … }Element or nilYour blockGetting the first match back
arr.index(x)Position or nil== or your blockGetting where the match is
arr.intersect?(other)true / falseeql? and hashAny of several values (Ruby 3.1+)
set.include?(x)true / falseeql? and hashMany lookups in a large collection
arr.any?false for [nil]TruthinessAvoid as a non-empty test: use empty?

Frequently asked questions

What is the Ruby version of Python's in or JavaScript's includes()?

It is array.include?(value), which returns true or false; member? is the same method. Ruby has no membership operator: x in array is valid syntax but it is pattern matching, so 2 in [1, 2] checks whether 2 matches an array of exactly 1 and 2, and is false.

Is in? a Ruby method?

No, value.in?(array) comes from Rails (ActiveSupport), as do exclude?, present? and blank?. In plain Ruby, 2.in?([1]) raises NoMethodError: undefined method 'in?' for an instance of Integer. Write array.include?(value) instead, which works the same with or without Rails.

How do I check that an array does not contain a value?

Negate include?: !array.include?(x), or put it after unless: puts "missing" unless array.include?(x). For a condition, use none? { |e| … }. exclude? is the Rails spelling and does not exist in plain Ruby.

How do I check if an array contains all the values of another array?

Subtract and check that nothing is left: (wanted - array).empty? is true when every wanted value is in array. wanted.all? { |w| array.include?(w) } reads the same way. With sets, wanted.to_set.subset?(array.to_set) does it too, and <= is the operator form.

Is include? case-sensitive?

Yes. ["Apple"].include?("apple") is false because it compares strings with ==. For a case-insensitive check, use a block: names.any? { |n| n.casecmp?("apple") }, or downcase both sides with names.map(&:downcase).include?(x.downcase).

Does include? work on hashes, strings and ranges?

Yes, with a different meaning on each. Hash#include? checks keys ({a: 1}.include?(:a) is true, include?(1) is false; use value? for values). String#include? looks for a substring, so "hello".include?("ell") is true. Range#include? checks whether a value lies in the range, as in (1..10).include?(5).

Run it yourself

Open any of these in the full Ruby editor to tweak, run and share.

Ruby playground