You have to specify the full condition on both sides of the or
.
if response == "a" or response == "A"
The two sides of the or
are not connected; Ruby makes no assumptions about what's on the right based on what's on the left. If the right side is the bare string "A"
, well, anything other than false
or nil
is considered "true", so the whole expression evaluates as "true" all the time. But Ruby notices that it's a string and not actually a boolean value, suspects you might not have specified what you meant to, and so issues the warning in the question.
You can also use a case
expression to make it simpler to do multiple tests against a single value; if you supply a list of multiple possibilities in a single when
, they are effectively or
ed together:
case response
when "a","A"
puts "ok"
when "b","B"
puts "awesome."
else
puts "I'm sorry. I did not get that. Please try again."
end
For the specific situation of ignoring alphabetic case, you could also just convert to either upper or lower before testing:
case response.upcase
when "A"
puts "ok"
when "B"
puts "awesome."
else
puts "I'm sorry, I did not get that. Please try again."
end