Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Collections Ruby Arrays Ruby Arrays

Why are there no commas between elements when using %W()?

Jason writes %W(milk eggs bread), why is there no need to add commas between each element? What happens if you want an element to have a space? Would you just add \s?

1 Answer

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

There are two ways to add spaces:

%w(yes no I\ don't\ know)

or

%W(yes no #{I don't know})

Notice that the second method requires the capital W. The first one works with both uppercase and lowercase W. It's like single and double quotes.

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

This is just short and fancy way of making an array of strings.

So it is assuming that you are trying to add multiple one word strings?

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

It's assuming you are adding strings without requiring you to use quotes or commas :). Spaces between the words are the only thing that requires an extra mile. But yeah, most use cases I've seen are lists of single words.

Excellent! Thanks for the help :)

hi maciej czuchnowski , thanks for sharing your advice for using spaces. i just wanted to ask that second method which u said isn't working for me. %W(yes no #{I don't know}). we can interpolate only variables that are already declared right?

rowend rowend
rowend rowend
2,926 Points

Manas sricharan VARRI Yes, you can only interpolate with variables declared previously. Or inside of interpolation sintax set the ruby expression to evaluate like: "#{5 + 1}"