Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby
communitycreator

What is array.include?() in Ruby?

okwudili onyejiaku

The array.include?() method is an array method that checks if an element is present in an array. It returns the Boolean value true if the element is present. Otherwise, it returns false.

Syntax

array.include?(element)

Parameter

  • element: This is the element to check for in the array.

Code

In the example below, several arrays were created and the include?() method was called on them with the elements to check. Then, the returned values are printed to the console.

# create arrays
array1 = [1, 2, 3, 4, 5]
array2 = ["a", "b", "c", "d", "e"]
array3 = ["Javascript", "Python", "Java", "C++", "Ruby"]
array4 = ["cat", "dog", "cow", "goat"]
array5 = ["", nil, "false", "true"]
array6 = [true, false]

# search elements with "include?()"
a = array1.include?(2)        # true
b = array2.include?("f")      # false
c = array3.include?("C")      # false
d = array4.include?("cat")    # true
e = array5.include?("")       # true
f = array6.include?(false)    # true

# print returned values to the console
puts a
puts b
puts c
puts d
puts e
puts f

RELATED TAGS

ruby
communitycreator
RELATED COURSES

View all Courses

Keep Exploring