How to Check if an Array Contains a Value in Ruby with include?
In Ruby, array.include?(value) returns true when at least one item of the array equals value, and false otherwise. The match is exact, so 'Customer' doesn't match 'customer'.
Syntax
<array>.include?(<value>)
| Part | What it does |
|---|---|
<array> | The array to search. |
<value> | The value to look for. Each item is compared with it using ==. |
| The result | true if some item equals <value>, otherwise false. |
The Ruby documentation: include? "Returns whether for some element element in self, object == element".
The Example
The example checks a user's roles, customer and beta-tester, for three values: customer, admin, and Customer with a capital C. Then it looks for customer twice more: in an array that holds the single role customer-support, and in that same role written as a plain string instead of an array.
Prerequisites
- Ruby. This guide uses Ruby 4.0.10.
Step 1: Write the Code
Create a file named main.rb:

roles = ['customer', 'beta-tester']
p roles.include?('customer')
p roles.include?('admin')
p roles.include?('Customer') # the match is exact, so case matters
p prints each result, true or false.
Step 2: Run It
ruby main.rb
true
false
false
The command is the same in zsh, bash and PowerShell.
Step 3: Pass a String Instead of an Array
Replace main.rb with a version that looks for customer in the role customer-support, first in an array, then in a string:
roles = ['customer-support']
p roles.include?('customer')
# The same role as a string, where an array of roles was expected.
roles = 'customer-support'
p roles.include?('customer')
ruby main.rb
false
true

What Happened
'customer'is in the array, soinclude?returnedtrue.'admin'isn't, so it returnedfalse.'Customer'returnedfalse:==compares strings exactly, capital letters included.['customer-support'].include?('customer')wasfalse: the array's only item,'customer-support', isn't equal to'customer'.'customer-support'.include?('customer')wastrue. A string has aninclude?of its own, which the Ruby documentation describes as "Returns whether self contains other_string". It looks for the text anywhere inside the string, so when roles arrive as a string, a role that only contains the wordcustomerpasses the check. Make sure the value is an array first, withis_a?(Array).
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Calling include? on a value that may be a string | String#include? finds text inside the string: 'customer-support'.include?('customer') was true | Check is_a?(Array) first |
Calling include? on a value that may be nil | undefined method 'include?' for nil (NoMethodError) | Check is_a?(Array) first |
| Expecting the match to ignore case | include?('Customer') was false for 'customer' | Compare with casecmp?, as in the FAQ below |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
undefined method 'include?' for nil (NoMethodError) | The value is nil, for example a missing field | Check is_a?(Array) first |
undefined method 'includes' for an instance of Array (NoMethodError) | includes is the JavaScript name | Use include?, with a question mark. Ruby itself suggested it: Did you mean? include? |
| A check passes for a value that only contains the word | The value is a string, not an array | Check is_a?(Array) first |
FAQ
Does include? compare types?
It compares with ==, so the answer depends on the types. In my check, [1, 2].include?(2.0) was true, because 2 == 2.0, but ['1', '2'].include?(2) was false, because a string never equals a number.
How do I ignore case?
Compare each item with casecmp?, which ignores case: roles.any? { |role| role.casecmp?('CUSTOMER') } returned true for the roles customer and beta-tester.
Conclusion
include? tells you whether an array holds a value, compared exactly with ==. Strings have an include? of their own that matches text inside them, so make sure the value really is an array before you rely on the answer.
Additional Resources
- Array#include? and String#include?, in the Ruby documentation.
