views:

85

answers:

2

In Rails guide this came up:

%w{ models }.each do |dir|

Could someone explain for me what %w{ models } means? Never seen it before. Is it ruby or rails specific.

Thanks

+5  A: 

%w{ foo bar baz } creates an array ["foo", "bar", "baz"], it's a shortcut to save typing some quotes and commas. %{ models } just creates an array ["models"], which does seem slightly superfluous, but is probably just for keeping the style consistent (?).

deceze
lol, beat me to it...
Brian
It could be that the reader is meant to mentally fill in the names of models. Maybe.
Chuck
Yay, my first accepted Ruby answer. The learning is paying off... ;D
deceze
wow, I left that as a comment when no one had answered because I was sure that no one would do such a thing...
Ed Swangren
@deceze. maybe the author has several models and each one has to have its own block.
never_had_a_name
@fayer Starting with an `.each do` does seem to suggest what @Chuck said: you're supposed to fill in your model names here. It's hard to say without any more context though.
deceze
Just a note that `%w[ a b c ]` is equivalent to `[ "a", "b", "c" ]` where `%[ a b c ]` is actually `" a b c "` since `%[...]` is an alternative way of quoting something, replacing `'...'`.
tadman
+4  A: 

%w allows you to create an array out of a string of words delimited by a space. here is an example:

irb(main):001:0> %w{ foo bar baz }.each { |word| puts word }
foo
bar
baz
=> ["foo", "bar", "baz"]

Here is a decent reference. It is a Ruby-ism, not specific to Rails

Brian
You have the better link though. :D
deceze