Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where in the Ruby language is %q, %w, etc., defined?

Tags:

ruby

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?

like image 900
Keith Bennett Avatar asked Nov 26 '10 17:11

Keith Bennett


People also ask

What is %W in ruby?

%w(foo bar) is a shortcut for ["foo", "bar"] . Meaning it's a notation to write an array of strings separated by spaces instead of commas and without quotes around them.

What does :: mean in ruby?

The :: is a unary operator that allows: constants, instance methods and class methods defined within a class or module, to be accessed from anywhere outside the class or module. Remember in Ruby, classes and methods may be considered constants too.

What are literals in ruby?

Any constant value which can be assigned to the variable is called as literal/constant. we use literal every time when typing an object in the ruby code. Ruby Literals are same as other programming languages, just a few adjustments, and differences here. These are following literals in Ruby.

Why is ruby called ruby?

Ruby is a predominantly feminine given name taken from the name of the gemstone ruby. The name of the gemstone comes from the Latin rubinus, meaning red. The ruby is the birthstone for the month of July. The name Ruby is taken from the name of the gemstone ruby.


2 Answers

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.

like image 111
the Tin Man Avatar answered Oct 12 '22 16:10

the Tin Man


They are “hard coded” in the parser; see

  • parse.y from the tip of Ruby 1.9.2 or
  • parse.y from the tip of Ruby 1.8.7.

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).

like image 23
Chris Johnsen Avatar answered Oct 12 '22 17:10

Chris Johnsen