Check if a value is present in an array in Ruby

Posted on

Question :

How to check if a certain value is contained in an array in Ruby? For example, I want to know if 'A' is present in the ['A','B','C'] vector.

    

Answer :

Use the include? method.

a = [ "a", "b", "c" ]
a.include?("b")   #=> true
a.include?("z")   #=> false

    

Leave a Reply

Your email address will not be published. Required fields are marked *