Skip to content

Working with Strings

Last updated on July 28th, 2024 at 04:48 pm

Single vs Double quotes

Ruby treats strings made with single quotes (') a bit differently than strings created with double quotes ("). Basically, if you want to use escape characters (such as \n for a new line) or string interpolation, this can only be done with strings delimited by double quotes. While you can put any characters you want in a string delimited with single quotes, the escaped characters will be printed exactly as they are shown in the string.

Ruby also allows you to create strings without using single or double quotes. %() and %Q() will create double quoted strings, while %q() will create single quoted strings.

Because you may sometimes want to have parenthesis in a string, you can use [], {}, -- and even ?? as the delimiters instead of ().

Multi-Line Strings

Ruby has several ways to create multi-line strings. It is worth noting that all of the methods below add a newline to each line of the multi-line string, with the exception of concatenation using \ or +.

Regular Heredoc

Heredocs allow you to create multi-line strings using a delimiter. You can use << followed by an identifier to open the string and the same identifier to close it. There are two common types of heredoc: regular and indented.

Indented Heredoc (with <<-)

Using <<- allows you to indent the closing delimiter for better code formatting.

Heredoc with String Interpolation

Heredocs also support string interpolation.

Using Double Quotes

Note that this also supports string interpolation.

Using Single Quotes

Using %(), %Q() and %q()

Note that %() and %Q() also support string interpolation, but %q() does not.

Concatenating Strings

Assignments are to Objects

One thing that you need to be aware of is that Ruby operates on objects. That is, if you assign one object to another, the assignee object takes on a reference of the assigned object. Have a look at the following code example:

The variable message is assigned a string, and then we assign message to nextMessage. In many languages, nextMessage would be assigned the value stored in message. In Ruby, nextMessage now points to the object pointed to by message. Any change to the object pointed to by message is automatically reflected in nextMessage. If we assign a new value to message, nextMessage still points to the String object that was originally assigned to message.

Freezing things

While there are many cases where you will want to manipulate string objects, it is often the case, such as with a constant, that you want a string object to reliably remain unchanged for its lifetime. To accomplish this, Ruby has the freeze method.

If you want to unfreeze a string, Ruby introduced the String#+@ operator which will check to see if the string is frozen. If it is, it will return a copy of the string. If it is not, it will return the string.

If you want to ensure that all strings within a file are frozen after creation, you can use what is called a pragma.

If you would like to enable frozen string for all files in a project,you can use the Ruby environment variable.

Be careful where you do this, however, as it is possible that some of the gems you use aren’t written in ways that treat string literals as frozen by default.

Actually, as you might expect with Ruby, because a string is just an object, you are really just freezing an object. So, if you can freeze a string, you can freeze any object. However – be advised that freezing only acts at the base level of the object. Any object contained within a frozen object can still be changed if it is not explicitly frozen as well.

Common String Functions

Concatenation

String concatenation is achieved using the + operator.

Naturally, you can concatenate any number of strings, using both literals and variables.

One disadvantage of using the + operator to concatenate strings is that each time you do this new objects are created. A more efficient method for concatenation is the << operator. You’ll notice that if you assign a string and then use the << operator to change it, the assigned object changes as well the original object.

String interpolation

Although string concatenation is a very easy to implement approach, it is suggested that wherever possible you use string interpolation instead. The reason for this is that if you attempt to concatenate an object that evaluates to nil, then the statement will throw an error. If you use string interpolation, the object that evaluates to nil will simply be ignored.

With string interpolation you can insert numeric values in the string without having to explicitly call to_s.

To use string interpolation, however, you need to use double quotes (") to create the string, not single quotes (').

Searching for text within a string

It is very common that we will want to know whether or not a string contains a given text. In Ruby, there are two basic ways of accomplishing this task. If you just want a true/false result as to whether or not the text is contained within a string, you can use the include? method.

Often, however, you will want to know where specific text starts within a string. For this you would use the index method.

Extracting text

A string in Ruby is actually treated as an array of characters. If you know the starting and ending indexes of a text within a string, you can extract that string using a range.

Naturally, the start and end points of the range can also be variables or expressions.

Pattern searches

Sometimes you may want to search a string for text that matches a specific pattern. One way of doing this in Ruby is to use Regular Expressions and the scan method. A very good example of this is found in stack overflow.

A more comprehensive explanation of pattern searching using regular expressions can be found on the Ruby Doc web site.

String Replacement

Ruby has some very interesting and uncommon methods for string replacement, the first of which we will cover are sub and gsub. As the name suggests, sub performs a substitution of one string for another, but only for the first instance. If you wish to substitute all instances of the string, you would use gsub for global substitution.

Interestingly, you can also replace strings by considering the source string as an index in the string’s array.

Doing, this, however, only assigns to the first instance of the string.

Because a String is conceptually thought of as an array of characters, you can access positions within the string as you would any array.

Unlike with most array functions, however, the string assignment doesn’t have to be exactly the same length as the string it is replacing.

If you wish to reverse the order of a string, you can use the reverse method.

Finally, there is the replace method, which is nothing more than the reassignment of the string contents. Notice that it doesn’t replace the object like a normal string assignment would.

Commonly used string methods

As of the writing of this documentation, the String class has over 180 methods. While that is clearly too much too cover for the purposes of this document, I will touch on some commonly used methods. For more detailed explanations of each method, please refer to either the Ruby pages of the API Doc website or the Ruby Doc website.

Method NamePurpose
length, sizeReturn the length of the string.
countCount the number of instances of a sub-string.
sub, sub!, gsub, gsub!, replace, tr, tr!, tr_s, tr_s!String substitution methods. sub for single instance substitutions, gsub for replacing all instances in a string.
match, match?Regular expression based string search.
chopReturns a new String with the last character removed.
chomp, chomp!Returns the string minus the pattern, if found at the end of the string.
delete, delete!Remove all instances of characters from a string
index, rindexReturn the index of the first (or last) occurrence of a pattern
splitSplit a string into an array of strings using a provided pattern. If not provided, the default separator is a space character. Note – the resulting array excludes the separator pattern. The pattern can be either a string or a regular expression.
delete_suffix, delete_suffix!, delete_prefix, delete_prefix!Remove suffix / prefix from string.
rjust, ljustCreate right / left justified strings.
upcase, upcase!, downcase, downcase!, swapcase, swapcase!, capitalize, capitalize!, reverse, reverse!String conversion routines.
insertInsert a sub-string.
strip, strip!, lstrip, lstrip!, rstrip, rstrip!Remove leading and trailing white space from a string.
to_s, to_i, to_f, to_r, to_enum, to_c, to_symConversion routines. Oddly enough, a string has a to_s and to_str .
casecmp, casecmp?String comparison functions.
start_with?, end_with?Does a string start or end with a given pattern?
chrReturns the first character of the string as a string.
charsReturns the string as an array of single characters.
bytesReturns the string as an array of bytes.
%Format the string. See this link for details.
squeeze, squeeze!Remove repetitions of characters from string.
each_line, each_byte, each_char, each_grapheme_cluster, each_codepointIterate through the string.
freezeMakes string immutable
dupClone a string