Ruby for Programmers – Part 3: Strings and Symbols

Now that we’re cool with Objects, lets go back to discovering the basic built-in classes of Ruby. Strings are very important in any language, so let’s work with them!

In Ruby you can make strings in three different ways:

Output:

The reason for the different methods is if having to escape quotes roughs your style. The big difference is in how Ruby interprets the single quotes vs. double quotes. In the single quotes, it will take what you written literally, and that’s it. In double quotes, it does processing on the string to see if you meant any special codes. For example, like most languages, you can use the code “\n” to denote a new line:

So as you can see, the single quote literally wrote ‘\n’ whereas the double quote made it the new line character. Note that we’re using “print” instead of “p”. Print will print the literal characters to the screen, but ‘p’ will prettify it first.

Double quotes also have interpolation (variables within strings):

Interpolation is very useful. You will be using this a lot.

Strings are cool, but what you really want is to do things to strings, so here’s just some example methods:

Just so it isn’t obvious, you can use “#” at the beginning to denote a comment, like in most other shell-type languages. (You can’t use C-style “//” for a comment.)

For the curious, there are more things you and do to a string list in the core documentation. This just scratches the surface, see the “%” for sprintf style stuff, or the centering, or stripping.

Also, just so it’s mentioned but it’s rarely used is the multiline string:

The differences are mostly based on preference. All three support interpolation and stuff.

Symbols

There are special strings in Ruby called Symbols. Think of them as Diet Strings, as they are stored differently. While a String is a lot like a C-based char[] array where it will store the data for each one (in fact, that’s how Ruby does it internally), Symbols have their data in the symbol table and looked up from there. This is very useful for when you’re passing around configuration or column data, which you want to add headers to the data.

For example, you might see something in Ruby like:

The “:yes” part is a symbol:

If you’re going to be passing information around, a Symbol is like a giant set of enums. That way you’re not creating a new string each time you want to pass information, when an enum could suffice for what that information is. Since it has a look up table, you can just simply convert it to a string, and it auto-converts on interpolation:

You shouldn’t be using Symbols as you would a String. But it makes sense when you have a need for enums or diet strings.

There are some instances where Ruby requires a Symbol instead of a String. For example due to the Objectness of Ruby, you can actually call any method using __send__, so the following are the same:

So the first two you are familiar with. In the previous part, I showed that since everything is an object, you can call everything’s function like an object. Ruby just assumes that if you do “object1 + object2”, it just interprets it as “object1.+(object2)” because it makes the code look pretty. But you can also have it run any method using “__send__” where it requires the name of the method as a symbol. (Since method names are stored internally as a symbol. Make sense, since method names are used often enough to be necessary as an enum or diet string.)

Anyway, if you see example code that uses Symbols, use that instead. Diet Strings are very useful for performance; if you’re using a string as a information token where a random integer could provide the same information (like an enum), use a symbol.