There are alternative forms of literal strings, arrays, regular
expressions, and shell commands that are specified using a generalized
delimited syntax.
All these literals start with a percent character,
followed by a single character that identifies the literal's type.
These
characters are summarized in Table 18.1 on page 200; the actual
literals are described in the corresponding sections later in this
chapter.
General delimited input
Type
|
Meaning
|
See Page
|
%q
|
Single-quoted string |
202 |
%Q , %
|
Double-quoted string |
202 |
%w
|
Array of tokens |
204 |
%r
|
Regular expression pattern |
205 |
%x
|
Shell command |
218 |
|
|
Following the type character is a delimiter, which can be any
character. If the delimiter is one of the characters ``
(
'',
``
[
'', ``
{
'', or ``
<
'', the literal consists of the
characters up to the matching closing delimiter, taking account of
nested delimiter pairs. For all other delimiters, the literal
comprises the characters up to the next occurrence of the delimiter
character.
%q/this is a string/
%q-string-
%q(a (nested) string)
|
Delimited strings may continue over multiple lines.
%q{def fred(a)
a.each { |i| puts i }
end}
|