October 2006
Intermediate to advanced
888 pages
16h 55m
English
A string in Ruby is simply a sequence of 8-bit bytes. It is not null-terminated as in C, so it can contain null characters. It may contain bytes above 0xFF, but such strings are meaningful only if some certain character set (encoding) is assumed. (For more information on encodings, refer to Chapter 4.
The simplest string in Ruby is single-quoted. Such a string is taken absolutely literally; the only escape sequences recognized are the single quote (\') and the escaped backslash itself (\\):
s1 = 'This is a string' # This is a string s2 = 'Mrs. O\'Leary' # Mrs. O'Leary s3 = 'Look in C:\\TEMP' # Look in C:\TEMP
A double-quoted string is more versatile. It allows many more escape sequences, such as backspace, tab, ...
Read now
Unlock full access