x6iae
x6iae

Reputation: 4164

Grouping words together in array literal `%w`

Is there any way to combine more than one word together in %w(foo bar) notation to get the resulting array as follows?

%w(foo bar foo or bar foo and bar) # => ["foo", "bar", "foo or bar", "foo and bar"]

Upvotes: 0

Views: 55

Answers (3)

sawa
sawa

Reputation: 168111

Not using %w notation, but with a similar spirit, using | instead of space for a delimiter:

"foo|bar|foo or bar|foo and bar".split("|")

Upvotes: 0

zwippie
zwippie

Reputation: 15515

It's possible by using \ as the space character:

%w(foo bar foo\ or\ bar foo\ and\ bar) => ["foo", "bar", "foo or bar", "foo and bar"]

Upvotes: 1

spickermann
spickermann

Reputation: 106932

Yes, there is a way. You can excape a whitespace with \:

%w(foo bar foo\ or\ bar foo\ and\ bar)
#=> ["foo", "bar", "foo or bar", "foo and bar"]

But I am not sure if this improves readability...

Upvotes: 2

Related Questions