Datatypes

Get familiar with Ruby's different datatypes.

While everything in Ruby is an object, some of the data types in Ruby have special syntax support, for defining literal values. In the preceding examples, we used some simple strings and even string concatenation.

Strings

The previous example also showed some Ruby string objects. One way to create a string object is to use string literals, which are sequences of characters between single or double quotation marks. The difference between the two forms is the amount of processing Ruby does on the string while constructing the literal. For single-quotes, Ruby does very little. With only a few exceptions, what we type into the single-quoted string literal becomes the string’s value. Ruby does more work with double-quotes. It looks for substitutions—sequences that start with a backslash character—and replaces them with a binary value. The most common of these is \n, which is replaced with a newline character. When we write a string containing a newline to the console, the \n forces a line break.

Ruby then performs expression interpolation in double-quoted strings. In the string, the sequence #{expression} is replaced by the value of the expression. We could use this to rewrite our previous method:

Get hands-on with 1400+ tech skills courses.