When The Solution in Easy

I always forget how helpful Ruby errors are, until I run into one and don’t remember what it means. I had this loop for a game of Blackjack that I’m making:

if play == “yes” || “y”
elsif play == “no” || “n”
puts “Sorry to hear that. Come back when you want to play!”
puts “Kate’s Jacked Game of Blackjack doesn’t understand.
Please enter (y)es or (n)o or q to quit.”

The code still runs, but as a reminder, I get this error: “warning: string literal in condition”

A quick Google search reminds me of this:

“The warning is saying that instead of a boolean or test, you have a string literal, ‘n’, which always evaluates to true. When you are writing input == “N” “n” ( internally Ruby sees it ( input == “N”) “n” ), it means “n” string object is always a truth value.”


Ruby is tricky in that it seems so simple on the outside, but really it’s complex and beautiful and sometimes needs to remind the developers writing in it of that fact. This error is telling me that this line:

elsif play == “no” || “n”

will always evaluate to true. I thought there must be some strange problem that I would need to fix. Maybe change the play method to be a boolean value and any other input would be invalid, or only allow users to input “n” OR “no” and nothing else but that wouldn’t bode well for user experience… Until it dawned on me.

Computers need to be told everything in order to work properly. play == “no” makes perfect sense, but on the other side of the || all it saw was “n”. It didn’t know what to do with a string of the letter n. So instead of making a bunch of unnecessary changes and being overly clever with my simple game, I added this:

elsif play == “no” || play == “n”

Problem solved. They say leaning to think like a computer is more important than learning the syntax of any language, and I agree 110%. Just remember kids, computers are awesome and can do a lot of things really fast, but man are they dumb.

Why you no work?
One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.