Where in the Ruby Language Is %Q, %W, etc., Defined

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

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

What is the %w thing in ruby?

Unsure about the "official" documentation but this is pretty good : http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals#The_.25_Notation

Whether you use {} [] () or <> does not matter except if your string contains this character e.g.:

%q{a closing parenthesis: ")"}

The syntax is pretty complex so remembering every variant is not very useful, but it can come in handy when you are hacking quickly and want to avoid taking care of escape characters manually.

What does %w(array) mean?

%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. You can find a list of ways of writing literals in zenspider's quickref.

Why is `each` in ruby not defined in the enumerable module?

From the documentation for Enumerable:

The Enumerable mixin provides collection classes with several traversal and searching methods, and with the ability to sort. The class must provide a method each, which yields successive members of the collection.

So the Enumerable module requires that classes which include it implement each on their own. All the other methods in Enumerable depend on each being implemented by the class which includes Enumerable.

For example:

class OneTwoThree
include Enumerable

# OneTwoThree has no `each` method!
end

# This throws an error:
OneTwoThree.new.map{|x| x * 2 }
# NoMethodError: undefined method `each' for #<OneTwoThree:0x83237d4>

class OneTwoThree
# But if we define an `each` method...
def each
yield 1
yield 2
yield 3
end
end

# Then it works!
OneTwoThree.new.map{|x| x * 2 }
#=> [2, 4, 6]

Is there a %w{ }--like way to create array of fixnums in Ruby?

Since the % notation seems to be one of those "bastard" Perl string handling inheritances in Ruby I strongly doubt it but you can save a couple of characters by

digits = %w{1 2 3 4 5 6 7 8 9 10}.map(&:to_i)

Is there a cleaner way to write this kind of Ruby conditional?


puts var1 if %w(hi bye well hello).include? var1

What are the pros and cons of Ruby's general delimited input? (percent syntax)

One good use for general delimited input (as %w, %r, etc. are called) to avoid having to escape delimiters. This makes it especially good for literals with embedded delimiters. Contrast the regular expression

  /^\/home\/[^\/]+\/.myprogram\/config$/

with

  %r|^/home/[^/]+/.myprogram/config$|

or the string

  "I thought John's dog was called \"Spot,\" not \"Fido.\""

with

  %Q{I thought John's dog was called "Spot," not "Fido."}

As you read more Ruby, the meaning of general delimited input (%w, %r, &c.), as well as Ruby's other peculiarities and idioms, will become plain.


I believe that is no accident that Ruby often has several ways to do the same thing. Ruby, like Perl, appears to be a postmodern language: Minimalism is not a core values, but merely one of many competing design forces.

What does %{} do in Ruby?


  1. "Percent literals" is usually a good way to google some information:

    • http://www.sampierson.com/articles/ruby-percent-literals
    • http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals#The_.25_Notation
  2. #{} is called "string interpolation".

Why are there so many slightly different ways to do the same thing in Ruby?


Why would I ever use non-interpolated strings?

When you don't want the interpolation, of course. For example, perhaps you're outputting some documentation about string interpolation:

'Use #{x} to interpolate the value of x.'
=> "Use #{x} to interpolate the value of x."

What advantage does the % syntax have over quoted literals?

It lets you write strings more naturally, without the quotes, or when you don't want to escape a lot of things, analogous to C#'s string-literal prefix @.

%{The % syntax make strings look more "natural".}
=> "The % syntax makes strings look more \"natural\"."

%{<basket size="50">}
=> "<basket size=\"50\">"

There are many other %-notations:

%w{apple banana #{1}cucumber}   # [w]hitespace-separated array, no interpolation
=> ["apple", "banana", "\#{1}cucumber"]

%W{apple banana #{1}cucumber} # [W]hitespace-separated array with interpolation
=> ["apple", "banana", "1cucumber"]

# [r]egular expression (finds all unary primes)
%r{^1?$|^(11+?)\1+$}
=> /^1?$|^(11+?)\1+$/

(1..30).to_a.select{ |i| ("1" * i) !~ %r{^1?$|^(11+?)\1+$} }
=> [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

%x{ruby --version} # [s]hell command
=> "ruby 1.9.1p129 (2009-05-12 revision 23412) [x86_64-linux]\n"

There's also %s (for symbols) and some others.

Why are there five ways to create string literals?

This isn't terribly unusual. Consider C#, for example, which has several different ways to generate strings: new String(); ""; @""; StringBuilder.ToString(), et cetera.



Related Topics



Leave a reply



Submit