I have the following which works well:
def steps
%w[hello billing confirmation]
end
steps.first
But I want to do this:
d
%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']
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}
%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.
%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.