SupremeA SupremeA - 5 months ago 10
Ruby Question

Check if a string is any of multiple items

I have a string

x = "student"


how do I check if "x" matches any of the items in a list of names that I have. The names are a fixed list.

names = ["teacher",
"parent",
"son",
"daughter",
"friend",
"classmate",
"principal",
"vice-principal",
"student",
"graduate"]


I tried setting names as a list and using any? to check the list but that seems to only work for an array and I have a string.

I am using Ruby 2.2.1 Also I only need it to return true or false if the item is in the list

Answer
names.include?(your_string)

If the string is inside the array it will return true