What does %w(array) mean?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Ruby, %w[...] is a shorthand for creating an array of strings without typing quotes and commas for every element. It is one of those small syntax features that looks odd at first, then becomes very readable once you know that Ruby is simply turning whitespace-separated words into string elements.
What %w Produces
The easiest way to understand %w is to compare it with ordinary array syntax.
Both arrays contain the same three strings. %w[...] is just shorter.
Ruby splits the content on whitespace and returns an array of strings. Each element behaves like a normal string:
This is why %w is often described as “word array” syntax.
Why People Use It
The main advantage is readability when the array consists of simple literal words.
Instead of this:
You can write this:
That removes punctuation noise and makes the intent obvious. It is especially pleasant for:
- symbol-like string lists
- whitelists and blacklists
- command names
- fixed option arrays in tests
If the values are all plain words, %w is usually a good fit.
Important Rules and Limitations
%w splits on whitespace. That means spaces create new array elements.
If you try this:
You do not get two full names. You get four separate strings. So %w is not appropriate when elements themselves contain spaces unless you escape them carefully.
Ruby does let you escape spaces inside the literal:
That works, but once the escaping starts to dominate, ordinary array syntax is often clearer.
Another important limitation is interpolation. %w does not evaluate interpolation expressions.
The second element is the literal text #{user}, not the value of the variable.
%w vs %W
Ruby also has %W[...], which looks similar but behaves differently. %W allows interpolation and escape processing.
In that example:
- '
%wkeeps#{user}as literal text' - '
%Wturns it into"alice"'
That difference is worth memorizing because the two forms are easy to confuse.
Delimiters Are Flexible
Ruby lets you choose delimiters other than square brackets. These forms are equivalent in spirit:
You might choose a different delimiter when the content itself contains bracket-like characters and you want the literal to stay easy to read.
When Not to Use %w
%w is elegant, but it is not mandatory. Avoid it when:
- elements contain lots of spaces
- interpolation is required
- escaping would make the literal harder to read
- the array elements are not all strings
For example, this is clearer as a normal array:
The shortest syntax is not always the best syntax. %w is valuable when it improves readability, not just because it is clever.
Common Pitfalls
The most common pitfall is expecting %w to interpolate variables. It does not. Use %W if you need interpolation.
Another mistake is forgetting that whitespace splits elements. That often surprises people when strings with spaces get broken apart.
A third issue is overusing escaped spaces inside %w. Once the array stops looking simple, normal array syntax is usually easier to maintain.
Finally, some developers think %w creates symbols because the syntax looks compact. It still creates strings, not symbols.
Summary
- '
%w[...]is Ruby shorthand for an array of strings.' - It splits the content on whitespace and returns normal string elements.
- '
%wis great for simple literal word lists.' - '
%W[...]is the related form that supports interpolation and escapes.' - If your values contain spaces or need heavy escaping, a regular array literal is often clearer.

