% w {models} .each do | dir | in Rails means?

In the Rails manual, this worked out:

%w{ models }.each do |dir|

Can someone explain to me what% w {models} means? Never seen this before. This is a ruby ​​or rails.

thank

+3
source share
2 answers

%w{ foo bar baz }creates an array ["foo", "bar", "baz"], this is a shortcut to save the input of some quotes and commas. %{ models }just creates an array ["models"]that looks a little redundant, but probably just to preserve the style (?).

+8
source

%wallows you to create an array from a string of words marked with 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's the link . This is non-Rails Ruby-ism

+4
source

Source: https://habr.com/ru/post/1758132/


All Articles