%q
RB-ST1123The %q
syntax is used to define a string literal with single quotes. It is unnecessary when the string does not contain any single quotes or when you want to use double quotes instead.
For example, %q{Hello world}
is equivalent to 'Hello world', but %q{Hello's world}
is necessary to avoid having to escape the single quote character: 'Hello's world'.
If you don't need to use single quotes or want to use double quotes, you can use other string literal syntaxes such as %{}
or "".
For example, %"Hello world"
is equivalent to "Hello world", and %{Hello world}
is also equivalent to "Hello world".
%q("hi")
# Use `%q` only for strings that contain both single quotes and double quotes
%q(\\foo\\)
# Use `%Q` only for strings that contain both single quotes and double quotes, or for dynamic strings that contain double quotes.
%Q(hi#{4})
'"hi"'
'\\foo\\'
"hi#{4}"