The include?()
method checks whether or not the given object is present in the array.
Array.include?(obj)
obj
: This the object to check.The method returns true
if the given object is present in the array. Otherwise, it returns false
.
arr = [ "educative", "edpresso", "courses", "shots" ]searchStr = "educative"puts arr.include?(searchStr)?"#{searchStr} is present in the array" :"#{searchStr} is not present in the array"searchStr = "hello"puts arr.include?(searchStr)?"#{searchStr} is present in the array" :"#{searchStr} is not present in the array"
arr
.searchStr
, that needs to be checked for its presence in arr
.include?()
method to find out whether or not searchStr
is present in arr
. Based on the return value of include?()
, we print if searchStr
is present in the array or not.