Check if an Array Contains a Value using member? method
The member?
method checks if the array contains the specified value and returns true
if it does, otherwise false
.
Syntax:
array.member?(value)
Example: In this example,we use member? method in the array to check if the array contains the value 3.Since 3 is present in array puts statement outputs true and similarly do for other members present in the array
# Define an array
array = [1, 2, 3, 4, 5]
# Check if array contains a value using member? method
puts array.member?(3) # Output: true
puts array.member?(6) # Output: false
Output
true false
How to Check if an Array Contains a Value in Ruby?
In this article, we will discuss how to check if an array contains a specific value in ruby. We can check if an array contains a specific value through different methods ranging from using include? method and member?
method to any? method
Table of Content
- Check if an Array Contains a Value using include? method
- Check if an Array Contains a Value using member? method
- Check if an Array Contains a Value using any? method
Contact Us