Skip to main content

How to Check if an Array Contains a Value in Ruby with include?

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
<array>.include?(<value>)
PartWhat it does
<array>The array to search.
<value>The value to look for. Each item is compared with it using ==.
The resulttrue 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:

main.rb, which sets roles to customer and beta-tester and prints include? for customer, admin and Customer with a capital C.

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​

Terminal
ruby main.rb
Output
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:

main.rb
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')
Terminal
ruby main.rb
Output
false
true

A zsh terminal where ruby main.rb prints true, false and false; for the changed file it prints false for the array that holds customer-support and true for the string customer-support.

What Happened​

  • 'customer' is in the array, so include? returned true. 'admin' isn't, so it returned false.
  • 'Customer' returned false: == compares strings exactly, capital letters included.
  • ['customer-support'].include?('customer') was false: the array's only item, 'customer-support', isn't equal to 'customer'.
  • 'customer-support'.include?('customer') was true. A string has an include? 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 word customer passes the check. Make sure the value is an array first, with is_a?(Array).

Common Mistakes​

MistakeWhat happensDo this instead
Calling include? on a value that may be a stringString#include? finds text inside the string: 'customer-support'.include?('customer') was trueCheck is_a?(Array) first
Calling include? on a value that may be nilundefined method 'include?' for nil (NoMethodError)Check is_a?(Array) first
Expecting the match to ignore caseinclude?('Customer') was false for 'customer'Compare with casecmp?, as in the FAQ below

Troubleshooting​

SymptomLikely causeFix
undefined method 'include?' for nil (NoMethodError)The value is nil, for example a missing fieldCheck is_a?(Array) first
undefined method 'includes' for an instance of Array (NoMethodError)includes is the JavaScript nameUse include?, with a question mark. Ruby itself suggested it: Did you mean? include?
A check passes for a value that only contains the wordThe value is a string, not an arrayCheck 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​