Menu
Coddy logo textTech

Include? Method

Part of the Fundamentals section of Coddy's Ruby journey — lesson 72 of 88.

When working with arrays, you often need to check whether a specific element exists before performing an action. Ruby's include? method answers this question directly, returning true or false.

fruits = ["apple", "banana", "orange"]

puts fruits.include?("banana")  # Outputs: true
puts fruits.include?("grape")   # Outputs: false

The method checks for an exact match, so it's case-sensitive with strings:

colors = ["Red", "Green", "Blue"]

puts colors.include?("Red")  # Outputs: true
puts colors.include?("red")  # Outputs: false

This method is particularly useful in conditional statements. Instead of looping through an array to find an element, you can simply ask if it's there:

allowed_users = ["alice", "bob", "charlie"]
current_user = "bob"

if allowed_users.include?(current_user)
  puts "Access granted"
else
  puts "Access denied"
end

The include? method works with any data type stored in the array, including numbers, symbols, and even other arrays.

challenge icon

Challenge

Easy

Read two lines of input:

  1. A list of available items in a store, as comma-separated strings (e.g., apple,banana,orange,milk,bread)
  2. A list of items on your shopping list, as comma-separated strings (e.g., banana,eggs,milk,cheese)

For each item on your shopping list, check if it's available in the store using the include? method. Print each item followed by Available or Not available based on whether it exists in the store's inventory.

Remember that include? checks for exact matches, so the comparison is case-sensitive.

For example, if the inputs are apple,banana,orange,milk,bread and banana,eggs,milk,cheese, the output should be:

banana - Available
eggs - Not available
milk - Available
cheese - Not available

If the inputs are Red,Green,Blue and red,Green,Yellow, the output should be:

red - Not available
Green - Available
Yellow - Not available

If the inputs are coffee,tea,juice and coffee,water,tea, the output should be:

coffee - Available
water - Not available
tea - Available

If the inputs are pen,pencil,eraser and pen, the output should be:

pen - Available

Cheat sheet

The include? method checks whether a specific element exists in an array, returning true or false:

fruits = ["apple", "banana", "orange"]

puts fruits.include?("banana")  # Outputs: true
puts fruits.include?("grape")   # Outputs: false

The method performs an exact match and is case-sensitive with strings:

colors = ["Red", "Green", "Blue"]

puts colors.include?("Red")  # Outputs: true
puts colors.include?("red")  # Outputs: false

Use include? in conditional statements to check for element presence:

allowed_users = ["alice", "bob", "charlie"]
current_user = "bob"

if allowed_users.include?(current_user)
  puts "Access granted"
else
  puts "Access denied"
end

The include? method works with any data type, including numbers, symbols, and arrays.

Try it yourself

# Read the store inventory (comma-separated items)
store_items = gets.chomp.split(",")

# Read the shopping list (comma-separated items)
shopping_list = gets.chomp.split(",")

# TODO: Write your code below
# For each item in the shopping list, check if it's available in the store
# using the include? method and print the result
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals