Q:

Error using Ruby OR in if

I have this simple condition in my ruby code:
if user.text != ("Empty" || "Damaged")

do something...
the thing is that when it is "Damaged" it still enters the loop, so I have to use this:
if user.text != "Empty"
    if user.text != "Damaged"
...
..

What is the correct syntax to make the first one work?
Thanks!

A:

Use this:
unless ["Empty", "Damaged"].include?(user.text)
  ...
end

The problem with your first approach is that a || b means that: if a != nil then it is a, else it is b. As you can see it is good for variables, not constants as they are rarely nil. Your expression ("Empty" || "Damaged") simply equals "Empty".
You want to use the this as a binary operator, the correct form would be:
if (user.text != "Empty") && (user.text != "Damaged")

The upper solution is just shorter. It consists of an array of elements you want to avoid, and you just simply check is the user.text is not present in it.

