Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




Ruby Programming
Previous Page Home Next Page


Ruby provides a number of mechanisms for creating literal strings. Each generates objects of type String. The different mechanisms vary in terms of how a string is delimited and how much substitution is done on the literal's content.

Single-quoted string literals (' stuff ' and %q/stuff/) undergo the least substitution. Both convert the sequence
into a single backslash, and the form with single quotes converts \' into a single quote.

'hello' hello
'a backslash \'\\\'' a backslash '\'
%q/simple string/ simple string
%q(nesting (really) works) nesting (really) works
%q no_blanks_here ; no_blanks_here

Double-quoted strings ("stuff", %Q/stuff/, and %/stuff/) undergo additional substitutions, shown in Table 18.2 on page 203.

Substitutions in double-quoted strings

\a Bell/alert (0x07) \nnn Octal nnn
\b Backspace (0x08) \xnn Hex nn
\e Escape (0x1b) \cx Control-x
\f Formfeed (0x0c) \C-x Control-x
\n Newline (0x0a) \M-x Meta-x
\r Return (0x0d) \M-\C-x Meta-control-x
\s Space (0x20) \x x
\t Tab (0x09) #{expr} Value of expr
\v Vertical tab (0x0b)

a  = 123
"\123mile" Smile
"Say \"Hello\"" Say "Hello"
%Q!"I said 'nuts'," I said! "I said 'nuts'," I said
%Q{Try #{a + 1}, not #{a - 1}} Try 124, not 122
%<Try #{a + 1}, not #{a - 1}> Try 124, not 122
"Try #{a + 1}, not #{a - 1}" Try 124, not 122

Strings can continue across multiple input lines, in which case they will contain newline characters. It is also possible to use here documents to express long string literals. Whenever Ruby parses the sequence <<identifier or <<quoted string, it replaces it with a string literal built from successive logical input lines. It stops building the string when it finds a line that starts with the identifier or the quoted string. You can put a minus sign immediately after the << characters, in which case the terminator can be indented from the left margin. If a quoted string was used to specify the terminator, its quoting rules will be applied to the here document; otherwise, double-quoting rules apply.

a = 123
print <<HERE
Double quoted \
here document.
Sum = #{a + 1}

print <<-'THERE'     This is single quoted.     The above used #{a + 1}     THERE
Double quoted here document.
Sum = 124
    This is single quoted.
    The above used #{a + 1}

Adjacent single- and double-quoted strings in the input are concatenated to form a single String object.

'Con' "cat" 'en' "ate" "Concatenate"

Strings are stored as sequences of 8-bit bytes,[For use in Japan, the jcode library supports a set of operations of strings written with EUC, SJIS, or UTF-8 encoding. The underlying string, however, is still accessed as a series of bytes.] and each byte may contain any of the 256 8-bit values, including null and newline. The substitution mechanisms in Table 18.2 on page 203 allow nonprinting characters to be inserted conveniently and portably.

Every time a string literal is used in an assignment or as a parameter, a new String object is created.

for i in 1..3
  print 'hello'.id, " "
537767360 537767070 537767040

The documentation for class String starts on page 363.
Ruby Programming
Previous Page Home Next Page

  Published under the terms of the Open Publication License Design by Interspire