Learn to Program

Chapter 3

Letters


String are the most deceptively easy concepts in programming. You have to remember when something is a string, when it is a number, object, method, etc. It can get hard. The easiest way to remember is if you got input from a user or put something into quotes (' ' or " ") then it is a string.

If you want to use a string as something else, like a number, then you have to change it to a number. Ruby will throw a TypeError exception if you try to use a string as a number or vice versa.

Escape characters are indicated by a backslash (\). Escape characters stop the character in question to be translated into syntax or a letter. You will usually use backslashes in regards to apostraphes.

  1. #incorrect

  2. puts 'You're great!'

  3. #correct

  4. puts 'You\'re great!'

If you want to put a backslash in with your text, you would also have to escape it.

On a final note of warning, the book gives the example that multiplying the string '2' by 5 gives you five twos: 22222. This doesn't work in all languages.