Reputation: 4970
So much of the Ruby language is methods rather than syntax, so I expected to find %q
, %w
, etc., defined as methods in the Kernel
class. However, they're not there.
So where are they defined? Are they language keywords?
Upvotes: 31
Views: 9021
Reputation: 11
[This note is in response to the discussion between Will and the Tin Man on May 30, 2012 on the topic of regexp literals looking like "%r.../" in the Ruby docs. I don't seem to be able to add a comment directly to that discussion, but perhaps someone with appropriate privs can move this info over there....]
The general Ruby class documentation is generated from RDoc
comments built into the source code for each class in question. Those source comments have correct syntax for the regular expression literals, as can been see when looking at the text documentation shown via ri Regexp
, using locally-generated HTML versions of the documentation, etc.
However, for a period of time this spring, the version of RDoc
used to generate the documentation pages on www.ruby-doc.org contained a bug that caused the regexp
literals to show up as "%r.../"
instead of "/.../"
. As of June 9, though, that bug was corrected and the "core" and "std-lib" pages were regenerated, so the regexp literals are displaying correctly again on that site.
(There are other categories of documentation on the site that are generated separately, but if any of those pages still show the incorrect format, they should also get fixed when they eventually get rebuilt.)
Upvotes: 1
Reputation: 160571
Programming Ruby mentions them in the chapter about strings.
Related to %q{}
and %Q{}
is %{}
which is the same as %Q{}
. Also, the delimiters "{}
" I show can be a matching pair of delimiters, so you could use []
, ()
, etc. %q{}
is the same as using single-quotes to delimit a string. %Q{}
is the same as using double-quotes, allowing embedded strings to be interpolated:
%q{foobar} # => "foobar"
%Q{foobar} # => "foobar"
asdf = 'bar' # => "bar"
%q{foo#{asdf}} # => "foo\#{asdf}"
%Q{foo#{asdf}} # => "foobar"
Also, there is %w{}
which splits a string using whitespace, into an array. For instance:
%w[a b c] # => ["a", "b", "c"]
%w{}
doesn't interpolate embedded variables:
%w[a b asdf] # => ["a", "b", "asdf"]
%w[a b #{asdf}] # => ["a", "b", "\#{asdf}"]
And %r{}
which defines a regular expression:
%r{^foo}.class # => Regexp
Finally there is %x{}
which acts like backticks, i.e. "``", passing the string to the underlying operating system. Think of it as "exec":
%x{date} # => "Fri Nov 26 15:08:44 MST 2010\n"
A lot of Ruby's ideas for these shortcuts come from Perl, only in Perl they use q{}
, qq{}
, qw{}
, qx{}
and qr{}
. The leading q
stands for "quote", and they are treated and documented as "quoting" operators if I remember right. Ruby's documentation needs to be expanded, and this particular set of tools could definitely use some help.
Upvotes: 31
Reputation: 224839
They are “hard coded” in the parser; see
The easiest way to find the code in question is to look for the second occurrence of str_sword
(Single-quoted WORDs). All the “delimited input” syntax is defined there: %Q
, %q
, %W
, %w
, %x
, %r
, and %s
(both versions referenced above define the same set of delimited input markers).
Upvotes: 26
Reputation: 94253
They're not keywords, they're macros. It's hard to find an official reference for them, but there's one here.
Upvotes: 5