Home »
Ruby programming
Array.include?(obj) Method with Example in Ruby
Ruby Array.include?(obj) Method: Here, we are going to learn about the Array.include?(obj) method with example in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on January 06, 2020
Ruby Array.include?(obj) Method
In the previous articles, we have seen how we can check whether two Array instances are identical or not with the help of <=> operator, == operator, and .eql? method? We have also seen different ways through which we can insert elements in the previously defined Array instances. Now, we can say that we have got a decent amount of knowledge about the Array class in Ruby language. In the last article, we have seen the implementation of the assoc() method but the assoc method only works for the Array object which is the collection of multiple Array objects.
For normal Array objects, we have Array.include?(obj) method. In this article, we will see how we can implement Array.include?() method? We will go through its syntax and some examples in the rest of the Array.
Method description:
This method is a Public instance method and belongs to the Array class which lives inside the library of Ruby language. This method is used to check whether an object is a part of the particular Array instance or not. It will search through the whole Array and gives you the result according to its search. Let us go through the syntax and demonstrating the program codes of this method.
If you are thinking about what it will return then let me tell you, it will return a Boolean value. The returned value will be true if it finds the object inside the Array and the return value will be false if it does not find the object to be the part of the Array instance.
Syntax:
array_instance.include?(obj)
Parameter(s):
This method only takes one parameter and that argument is nothing but an object whose presence we want to check.
Example 1:
=begin
Ruby program to demonstrate include method
=end
# array
array1 = [1,"Ramesh","Apple",12,true,nil,"Satyam","Harish"]
# input element to search
puts "Enter the element you want to search"
ele = gets.chomp
# checking
if array1.include?(ele) != false
puts "Element found"
else
puts "Element not found"
end
Output
RUN 1:
Enter the element you want to search
Apple
Element found
RUN 2:
Enter the element you want to search
Mango
Element not found
Explanation:
In the above code, you can observe that we are invoking the include method on the normal Array instance. It has returned true when it found the presence of an object in the Array object which is entered by the user.
Example 2:
=begin
Ruby program to demonstrate include method
=end
# arrays
array1 = [1,"Ramesh","Apple",12,true,nil,"Satyam","Harish"]
array2 = ["Akul","Madhu","Ashok","Mukesh",788]
array3 = ["Orange","Banana","Papaya","Apricot","Grapes"]
# main array
arraymain = [array1,array2,array3]
# input element to search
puts "Enter the element you want to search"
ele = gets.chomp
# checking
if arraymain.include?(ele) != false
puts "Element found"
else
puts "Element not found"
end
Output
Enter the element you want to search
Array1
Element not found
Explanation:
In the above, you can verify that include method does not work upon Array instance which is the collection of multiple Array instances. It will return false even if the object is a part of the Array instance.