tags:

views:

377

answers:

5

I don't understand why some people use the percentage syntax a lot in ruby.

For instance, I'm reading through the ruby plugin guide and it uses code such as:

%w{ models controllers }.each do |dir|
  path = File.join(File.dirname(__FILE__), 'app', dir)
  $LOAD_PATH << path
  ActiveSupport::Dependencies.load_paths << path
  ActiveSupport::Dependencies.load_once_paths.delete(path)
end

Every time I see something like this, I have to go and look up the percentage syntax reference because I don't remember what %w means.

Is that syntax really preferable to ["models", "controllers"].each ...?

I think in this latter case it's more clear that I've defined an array of strings, but in the former - especially to someone learning ruby - it doesn't seem as clear, at least for me.

If someone can tell me that I'm missing some key point here then please do, as I'm having a hard time understanding why the percent syntax appears to be preferred by the vast majority of ruby programmers.

+2  A: 

As the size of the array grows, the %w syntax saves more and more keystrokes by not making you type in all the quotes and commas. At least that's the reason given in Learning Ruby.

Bill the Lizard
It's also useful to avoid escaping `"` in strings.
kejadlen
Saving the keystrokes is irrelevant. Code only gets written once, anyway. It is, however, read *many* times and that's where reducing visual clutter *really* shines.
Jörg W Mittag
@Jorg: One of Ruby's priorities is developer happiness/productivity. Saving unnecessary keystrokes is definitely up there on the list.
thenduks
I have to agree with Jorg, I think saving keystrokes is probably the worst reasons for doing something such as this. I don't however think strings in an array are visually cluttered. I can definitely see the use for regex and escaping quotes, but straight up strings? seems a bit overkill.
brad
In this case saving keystrokes and reducing visual clutter are both achieved by the same mechanism, so does it really matter? The result is fewer characters on the screen, whether you're typing or reading makes little difference.
Bill the Lizard
A: 

I agree that most of the time %w is unnecessarily and less readable than [] + ''. However, it does have some (very limited) usefulness in one-off command-line programs, where it saves the effort of having to escape multiple quotes (and such) when the program text itself is already quoted for the shell.

In “regular” programs I would not consider the saved keystrokes to be worth the loss of consistency and legibility (as in the example given by the OP), especially as it also introduces some problems (e.g. the inability to use #-comments to remove lines from long, multi-line arrays, for which this syntax would otherwise have the greatest benefits).

Arkku
A: 

The %w syntax shaves 3 characters off each item in the list... can't beat that!

alt text

JRL
+11  A: 

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.

Wayne Conrad
I suppose the last one was meant to be %Q{...} otherwise it's a regexp. Difference between upper and lower case (%R vs. %r) is only if interpolations are resolved.
hurikhan77
@hurikhan77, yes, that's what I meant. Lizard brain, the creature of habit that it is, typed %R when I wasn't looking. Thank you for catching that.
Wayne Conrad
+2  A: 

It's easy to remember: %w{} is for "words", %r{} for regexps, %q{} for "quotes", and so on... It's pretty easy once you build such memory aids.

hurikhan77