• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Ruby / Ruby include Method – Check if Item in Array

Ruby include Method – Check if Item in Array

October 19, 2022 Leave a Comment

We can use the Ruby include? method to see if an item is contained in a given array. This is pretty straightforward. Here is the code below.

some_array.include?("item")

The include? method will return either true or false based on whether the item is found in the array.

Let’s see a simple example of this below.


Here we will have a simple array of numbers, and just use the include? method to see if a number exists in the array.

nums_array = [1,2,3,4,5]

puts nums_array.include?(2)
puts nums_array.include?(6)

#Output
true
false

Let’s take a look at a couple more examples below.

Using the Ruby include? Method on Hashes and Arrays

In this first example, we will have an array of random object types. We will simply use the include? method to check what it returns when searching for various items in the array.

random_array = ["This","false",true,4,"5"]

puts random_array.include?("This")
puts random_array.include?("this")
puts random_array.include?(false)
puts random_array.include?(true)
puts random_array.include?(5)

#Output
true
false
false
true
false

And let’s take a look at using the include? method on a hash in Ruby.

random_hash = {"first_name" => "Tom", "last_name" => "Smith", "age" => 5}

puts random_hash.include?("last_name")
puts random_hash.include?("Tom")
puts random_hash.include?("id")

#Output
true
false
false

Notice that with Hashes, the include? method will just check the key values and not the values of the key-value pairs.

Hopefully this article has been useful for you to learn how the Ruby include? method works.

Other Articles You'll Also Like:

  • 1.  Using Ruby to Print to Console
  • 2.  How to Print Without Newline in Ruby
  • 3.  Ruby has_key Method – Check if Hash Has Key
  • 4.  Using Ruby to Convert Array to String
  • 5.  Using Ruby to Convert String to Array
  • 6.  Ruby has_value Method – Check if Hash Has Value
  • 7.  How to Square a Number in Ruby
  • 8.  Ruby puts vs print
  • 9.  Using Ruby to Reverse String
  • 10.  Get the Sum of an Array in Ruby

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy