- 2.1 Representing Ordinary Strings
- 2.2 Representing Strings with Alternate Notations
- 2.3 Using Here-Documents
- 2.4 Finding the Length of a String
- 2.5 Processing a Line at a Time
- 2.6 Processing a Character or Byte at a Time
- 2.7 Performing Specialized String Comparisons
- 2.8 Tokenizing a String
- 2.9 Formatting a String
- 2.10 Using Strings as IO Objects
- 2.11 Controlling Uppercase and Lowercase
- 2.12 Accessing and Assigning Substrings
- 2.13 Substituting in Strings
- 2.14 Searching a String
- 2.15 Converting Between Characters and ASCII Codes
- 2.16 Implicit and Explicit Conversion
- 2.17 Appending an Item onto a String
- 2.18 Removing Trailing Newlines and Other Characters
- 2.19 Trimming Whitespace from a String
- 2.20 Repeating Strings
- 2.21 Embedding Expressions within Strings
- 2.22 Delayed Interpolation of Strings
- 2.23 Parsing Comma-Separated Data
- 2.24 Converting Strings to Numbers (Decimal and Otherwise)
- 2.25 Encoding and Decoding <tt>rot13</tt> Text
- 2.26 Encrypting Strings
- 2.27 Compressing Strings
- 2.28 Counting Characters in Strings
- 2.29 Reversing a String
- 2.30 Removing Duplicate Characters
- 2.31 Removing Specific Characters
- 2.32 Printing Special Characters
- 2.33 Generating Successive Strings
- 2.34 Calculating a 32-Bit CRC
- 2.35 Calculating the SHA-256 Hash of a String
- 2.36 Calculating the Levenshtein Distance Between Two Strings
- 2.37 Encoding and Decoding Base64 Strings
- 2.38 Expanding and Compressing Tab Characters
- 2.39 Wrapping Lines of Text
- 2.40 Conclusion
2.3 Using Here-Documents
If you want to represent a long string spanning multiple lines, you can certainly use a regular quoted string:
str = "Once upon a midnight dreary, While I pondered, weak and weary..."
However, the indentation will be part of the string.
Another way is to use a here-document, a string that is inherently multiline. (This concept and term are borrowed from older languages and contexts.) The syntax is the << symbol, followed by an end marker, then zero or more lines of text, and finally the same end marker on a line by itself:
str = <<EOF Once upon a midnight dreary, While I pondered weak and weary,... EOF
Be careful about things such as trailing spaces on the final end marker line. Current versions of Ruby will fail to recognize the end marker in those situations.
Note that here-documents may be “stacked”; for example, here is a method call with three such strings passed to it:
some_method(<<STR1, <<STR2, <<STR3) first piece of text... STR1 second piece... STR2 third piece of text. STR3
By default, a here-document is like a double-quoted string—that is, its contents are subject to interpretation of escape sequences and interpolation of embedded expressions. But if the end marker is single-quoted, the here-document behaves like a single-quoted string:
str = <<'EOF' This isn't a tab: \t and this isn't a newline: \n EOF
If a here-document’s end marker is preceded by a hyphen, the end marker may be indented. Only the spaces before the end marker are deleted from the string, not those on previous lines:
str = <<-EOF Each of these lines starts with a pair of blank spaces. EOF
To delete the spaces from the beginning of each line, we need another method. The ActiveSupport gem (included in Rails) defines a strip_heredoc method that works similarly to this one:
class String def strip_heredoc # Find the margin whitespace on the first line margin = self[/\A\s*/] # Remove margin-sized whitespace from each line gsub(/\s{#{margin.size}}/,"") end end
The amount of whitespace before the start of the first line is detected, and that amount of whitespace is then stripped off of each line. It’s used in this way:
str = <<end.strip_heredoc This here-document has a "left margin" set by the whitespace on the first line. We can do inset quotations here, hanging indentions, and so on. end
The word end is used naturally enough as an end marker. (This, of course, is a matter of taste. It looks like the reserved word end but is really just an arbitrary marker.) Many text editors use the end marker as a hint for syntax highlighting. As a result, using <<SQL or <<RUBY can make it dramatically easier to read blocks of code inside here-docs in those editors.