Monday 30 December 2019

Check if a value exists in an array in Ruby



I have a value 'Dog' and an array ['Cat', 'Dog', 'Bird'].




How do I check if it exists in the array without looping through it? Is there a simple way of checking if the value exists, nothing more?


Answer



You're looking for include?:



>> ['Cat', 'Dog', 'Bird'].include? 'Dog'
=> true

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...