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: falseThe 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: falseThis 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"
endThe include? method works with any data type stored in the array, including numbers, symbols, and even other arrays.
Challenge
EasyRead two lines of input:
- A list of available items in a store, as comma-separated strings (e.g.,
apple,banana,orange,milk,bread) - 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 availableIf the inputs are Red,Green,Blue and red,Green,Yellow, the output should be:
red - Not available
Green - Available
Yellow - Not availableIf the inputs are coffee,tea,juice and coffee,water,tea, the output should be:
coffee - Available
water - Not available
tea - AvailableIf the inputs are pen,pencil,eraser and pen, the output should be:
pen - AvailableCheat 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: falseThe 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: falseUse 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"
endThe 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 resultThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 42Variables and Data Types
Numbers and VariablesString Data TypeBoolean Data TypeSymbol Data TypeChecking Data TypesNaming ConventionsRecap - Variable Creation8Loops
For Loop with RangesWhile LoopBreakNextRecap - FactorialTimes LoopUntil LoopNested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators6Basic IO
Output with putsOutput with print and pOutput With VariablesInput with getsChomp MethodType ConversionRecap - Age CalculatorRecap - True or False