What is array.include?() in Ruby?
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 thearray.
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 arraysarray1 = [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) # trueb = array2.include?("f") # falsec = array3.include?("C") # falsed = array4.include?("cat") # truee = array5.include?("") # truef = array6.include?(false) # true# print returned values to the consoleputs aputs bputs cputs dputs eputs f