Strings
There are several ways to express a string literal.
Single-quoted strings
s = 'This is a string'
Escaping (use a backslash character)
\’ | ’ |
\\ | \ |
A backslash is only special (used for escaping) if the character that follows it is a quote or a backslash!
s = '\'' # '
s = '\\' # \
s = '\\\'' # \'
s = '\1' # \1
s = '\\1' # \1
s = '\\\1' # \\1
s = '"' # " : no escaping necessary
s = 'a\nb' # a\nb : it is not possible to express the newline character by escaping (\n)
s = '#{a}' # #{a} : does NOT perform string interpolation
Single-quoted string extended over multiple lines (contains newline characters):
s = 'This is a string
and this is the next line'
=begin
This is a string
and this is the next line
=end
In single-quoted strings it is not possible to escape the newline:
s = 'This is a string \
and this not on the same line'
=begin
This is a string \
and this is not on the same line
=end
A long single-quoted string without embedded newlines:
s = 'This is ' \
'a very, ' \
'very long string ' \
'and it does not contain any newline.'
=begin
This is a very, very long string and it does not contain any newline.
=end
Double-quoted strings
s = "This is a string"
Escaping
\x | A backslash before any character x is equivalent to the character x by itself, unless x is a line terminator or one of the special characters abcefnrstuvxCM01234567 . This syntax is useful to escape the special meaning of the \ , # , and “ characters. |
\” | ” | ||||||
\\ | \ | ||||||
\a | <BEL> | Bell character | ASCII code 7 | equivalent to | \007 | \C-g | \cg |
\b | <BS> | Backspace character | ASCII code 8 | equivalent to | \010 | \C-h | \ch |
\e | <ESC> | Escape character | ASCII code 27 | equivalent to | \033 | \C-[ | \c[ |
\f | <FF> | Form Feed character | ASCII code 12 | equivalent to | \014 | \C-l | \cl |
\n | <LF> | Line Feed character, Newline character | ASCII code 10 | equivalent to | \012 | \C-j | \cj |
\r | <CR> | Carriage Return character | ASCII code 13 | equivalent to | \015 | \C-m | \cm |
\s | <SP> | Space character | ASCII code 32 | equivalent to | \040 | ||
\t | <HT> | Horizontal Tab character | ASCII code 9 | equivalent to | \011 | \C-i | \ci |
\v | <VT> | Vertical Tab character | ASCII code 11 | equivalent to | \013 | \C-k | \ck |
\nnn | The byte nnn, where nnn is three octal digits between 000 and 377. |
\nn | Equivalent to \0nn, where nn is two octal digits between 00 and 77. |
\n | Equivalent to \00n, where n is an octal digit between 0 and 7. |
\unnnn | Unicode character. The Unicode codepoint nnnn, where each n is one hexadecimal digit. Leading zeros may not be dropped, all four digits are required. |
\u{hexdigits} | Unicode characters. The Unicode codepoint(s) specified by hexdigits. |
\xnn | The byte nn, where nn is two hexadecimal digits between 00 and FF (ff). Lowercase and uppercase characters are allowed. |
\xn | Equivalent to \x0n, where n is a hexadecimal digit between 0 and F (f). |
\C-x | Used to represent control characters. Zeroing the sixth and seventh bit of the code of x. |
\cx | Shorthand for \C-x. |
\M-x | Used to represent meta characters. Setting the high bit of the code of x. |
\M-\C-x | Used to represent a combination of meta and control character. |
\<eol> | <eol> (end of line, in your source code file, before the newline character); A backslash before a line terminator escapes the terminator. Neither the backslash nor the terminator appear in the string. |
Does perform string interpolation: #{<ruby expression>}
Examples:
s = "\"" # "
s = "'" # ' : no escaping necessary
s = "She said \"Yeah!\"" # She said "Yeah!"
s = "a quoted backslash \"\\\"" # a quoted backslash "\"
s = "Pi: \u03c0 = π" # Pi: π = π
s = "\u{49 20 6c 6f 76 65 20 74 68 65 20 6e 75 6d 62 65 72 20 3c0 21}" # I love the number π!
a = "nested string"
s = "Hello #{a}" # Hello nested string : DOES perform string interpolation
s = "Hello \#{a}" # Hello #{a}
s = "Hello #\{a}" # Hello #{a}
s = "#{n=2;m=3;n**m}" # 8
Double-quoted string extended over multiple lines (contains newline characters):
s = "This is a string
and this is the next line"
=begin
This is a string
and this is the next line
=end
In double-quoted strings it is possible to escape the (end of line) newline:
s = "This is a string \
and this is on the same line"
=begin
This is a string and this is on the same line
=end
Generalized quoting syntax
Arbitrary delimiters for string literals.
%q | follows single-quoted string rules |
%Q | follows double-quoted string rules |
% | follows double-quoted string rules (the Q is optinal) |
Matching opening and closing pairs of delimiters for (), [], {}, <>, otherwise the closing delimiter is the same as the opening delimeter.
You can use properly nested pairs of delimiters without escaping.
s = %q/recognized as 'single'-quoted string/ # recognized as 'single'-quoted string
s = %Q!recognized as "double"-quoted string! # recognized as "double"-quoted string
s = %|this is "also" recognized as a double-quoted string| # this is "also" recognized as a double-quoted string
s = %("Seconds per hour": #{60*60}) # "Seconds per hour": 3600
s = %{"Minutes per day": #{60*24}} # "Minutes per day": 1440
s = %(this is a (nested) string) # this is a (nested) string
s = %(no properly nested pairs here: a\) topic 1) # no properly nested pairs here: a) topic 1
Here document - Heredoc
Used for long strings. Does not use delimiters as single characters. Therefore no need to care about escaping delimiters.
Allows you to specify an arbitrary and unambiguous sequence of characters to act as the delimiter for the string.
<< | the terminating string must start at the beginning of a line (in column one) |
<<- | the terminating string can be indented |
<<~ | the indentation of the least-indented line will be removed from each line of the content - Ruby >= 2.3 |
If the terminator is defined as a single-quoted string, then single-quoting rules apply:
s = <<'EndOfString'
This is recognized
as a single-quoted string.
Time: #{Time.now}
No need to escape ' (a single quote) and no need to escape \ (the backslash)
EndOfString
=begin
This is recognized
as a single-quoted string.
Time: #{Time.now}
No need to escape ' (a single quote) and no need to escape \ (the backslash)
=end
Otherwise double-quoting rules apply:
s = <<EndOfString
This is recognized
as a double-quoted string.
Result: #{a=3;b=5;a+b}
There is no need to escape " (a double quote), but you need to escape \\ (the backslash)
EndOfString
=begin
This is recognized
as a double-quoted string.
Result: 8
There is no need to escape " (a double quote), but you need to escape \ (the backslash)
=end
Example for <<-
s = <<-EOS
This is recognized
as a double-quoted string.
<<- : the terminating string can be indented
EOS
=begin
This is recognized
as a double-quoted string.
<<- : the terminating string can be indented
=end
Example for <<~
s = <<~EOS
This is a
very long string
one more, result: #{a=3;b=4;a*b}
<<~ : the indentation of the least-indented line will be removed from each line of the content - Ruby >= 2.3
the terminating string can be indented
EOS
=begin
This is a
very long string
one more, result: 12
<<~ : the indentation of the least-indented line will be removed from each line of the content - Ruby >= 2.3
the terminating string can be indented
=end
Multiple here documents on a single line:
s = <<STRING1 + <<-STRING2
First string
STRING1
and second string
STRING2
=begin
First string
and second string
=end
s = <<STRING1.chop + <<~STRING2
First string
STRING1
\ and second string
STRING2
=begin
First string and second string
=end
s = (<<STRING1.chop + <<~STRING2.chop + " and third string").upcase
First string
STRING1
\ and second string
STRING2
=begin
FIRST STRING AND SECOND STRING AND THIRD STRING
=end
You can use spaces in the terminator:
s = <<'A very special and unique terminator'
My string
A very special and unique terminator
=begin
My string
=end
This really looks weird, doesn’t it? 😲
Character literals
Characters are simply strings of length 1.
For single characters preceded with a question mark (?).
s = ?a # a
s = ?\n # <newline character>
s = ?\u03c0 # π
s = ?\u20ac # €