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 trialRyan Egan
5,324 PointsWhy 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
36,441 PointsThere 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
36,441 PointsMaciej Czuchnowski
36,441 PointsThis is just short and fancy way of making an array of strings.
Ryan Egan
5,324 PointsRyan Egan
5,324 PointsSo it is assuming that you are trying to add multiple one word strings?
Maciej Czuchnowski
36,441 PointsMaciej Czuchnowski
36,441 PointsIt'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.
Ryan Egan
5,324 PointsRyan Egan
5,324 PointsExcellent! Thanks for the help :)
Manas sricharan VARRI
8,242 PointsManas sricharan VARRI
8,242 Pointshi 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
2,926 Pointsrowend rowend
2,926 PointsManas 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}"