I am creating a takeaway restaurant database, which can be accessed by the Managers of each store. One of the database tables in called Product, and it has a column called product_name. The product_name is a string with multiple words. I'm trying to check whether the product_name contains meat, and then tell the user whether it is or is not suitable for vegetarians.
This is the code I have. I was trying to create a meats array, and check product_name against it. I don't think the if not statement is correct.
class Checkveg
def self.runcheck(product_name)
meats = ["lamb", "beef", "pork", "prawn", "chicken", "fish"]
meats.each { |item|
if product_name.include? item
puts "Not suitable for vegans or vegetarians"
end
}
puts "Suitable for vegans or vegetarians" if not meats.include? product_name
end
end
**UPDATE:
I was able to fix it
noVeg = false
meats = ["lamb", "beef", "pork", "prawn", "chicken", "fish"]
meats.any? { |item|
if product_name.include? item
noVeg = true
break
end
}
if noVeg == true
puts "Not suitable for vegetarians"
else
puts "Suitable for vegetarians"
end
meats.include? xis going to be true ifxis in the arraymeats. Do you expectproduct_nameever to be an element of the array? Also, look up the Ruby Array class. There are other interesting methods such as.any?or.all?that may be of interest for your problem and make it simpler.puts "Suitable for vegans or vegetarians" if not meats.include? product_name. Would that also work, even if the product_name is a string with multiple words?meats.include? product_namewill be true only ifproduct_nameis an element of the arraymeats. I suspect that it never is. Also, see my prior comment about.any?and.all?.