tags:

views:

383

answers:

3

I'm looking at the documentation for FileUtils. I'm confused by the following line:

FileUtils.cp %w(cgi.rb complex.rb date.rb), '/usr/lib/ruby/1.6'

What does the %w mean? Can you point me to the documentation?

+8  A: 

%w(foo bar) is a shortcut for ["foo", "bar"]. Meaning it's a notation to write an array of strings seperated by spaces instead of commas and without quotes around them. You can find a list of ways of writing literals in zenspider's quickref.

sepp2k
Also, the parenthesis can be almost any other character such as square brackets %w[...], curly braces %w{...} or even something like exclamation marks %w!...!. All of these have the same behavior (returning an array).
ryanb
The easiest way to mnemonically remember what this means is "Whitespace (w) separated array".
Julik
+4  A: 

I think of %w() as a "word array" - the elements are delimited by spaces.

There are other % things:

  • %r() is another way to write a regular expression.
  • %q() is another way to write a single-quoted string (and can be multi-line, which is useful)
  • %Q() gives a double-quoted string
  • %x() is a shell command

I don't know any others, but there may be some lurking around in there...

Mike Woodhouse