Rails - Using %W

匿名 (未验证) 提交于 2019-12-03 02:50:02

问题:

I have the following which works well:

def steps     %w[hello billing confirmation] end  steps.first 

But I want to do this:

  def step_title     %w['Upload a photo' 'Billing Info' 'Confirmation Screen']   end  steps.first 

How does %w allow for that? I tried a google search but google is weak with these types of characters.

Thanks

回答1:

%w creates an "array of words," and uses whitespace to separate each value. Since you want to separate on another value (in this case, whitespace outside sets of quotation marks), just use a standard array:

['Upload a photo', 'Billing Info', 'Confirmation Screen'] 


回答2:

%w() is 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.



回答3:

You can also use the backslash to escape spaces:

%w@foo\ bar bang@ 

is the same as:

[ 'foo bar', 'bang' ] 

In your example I wouldn't use the %w notation, because it's not that clear.

PS. I do like mixing the delimiter characters, just to annoy team members :) Like this:

%w?foo bar? %w|foo bar| %w\foo bar\ %w{foo bar} 


回答4:

%w[hello billing confirmation] is syntax sugar for ["hello", "billing", "confirmation"]. It tells Ruby to break up the input string into words, based on the whitespace, and to return an array of the words.

If your specific use case means the values in the array are permitted to have spaces, you cannot use %w.

In your case, ['Upload a photo', 'Billing Info', 'Confirmation Screen'] suffices.



回答5:

One reason that you would want to use %w( xxx) over ["xxx", "xx1"] is that with %w you can format the contents for readability. For example, if you have many static values that are hard-coded in an array, they are much easier to read when stacked up like this:

         assets.precompile += %w(                                  bootstrap-datepicker.js                                 datepicker.css                                 flower_chart.js                                 jquery.flower-sort.js                                 bootstrap-timepicker.js                                 bootstrap-timepicker.css                                 admin.css                                 admin.js                                 flower_feature.js                                 jquery.flower.tooltip.min.js                                 jquery.infinite-flowers.js                                 data-flowers.bootstrap.min.js                                 jquery.monkey-flowers.min.js ) 

than flattened and surrounded by quotes and commas like this

["bootstrap-datepicker.js", "datepicker.css", "flower_chart.js", "jquery.flower-sort.js", "bootstrap-timepicker.js", "bootstrap-timepicker.css", "admin.css", "flower_feature.js", "jquery.flower.tooltip.min.js", "jquery.infinite-flowers.js", "data-flowers.bootstrap.min.js", "jquery.monkey-flowers.min.js"] 

yet, both result in the exact same array.



文章来源: Rails - Using %W
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!