How to Write an Array of Strings in Ruby with the %w Percent Literal
In Ruby, %w[customer beta-tester] is a short way to write ['customer', 'beta-tester']: an array of strings, written as words separated by spaces, with no quotes and no commas. Each word becomes one string.
Syntax
%w[<word> <word> ...]
| Part | What it does |
|---|---|
%w | Starts an array of strings. The lowercase w doesn't fill in #{...}; an uppercase %W does. |
[ and ] | Mark where the words start and end. %w(...) and %w{...} work the same way. |
<word> | One string, written without quotes. Spaces, tabs and new lines separate the words, and a backslash before a space keeps the space inside a word. |
The Ruby documentation: "You can write an array of strings as whitespace-separated words with %w (non-interpolable) or %W (interpolable)".
The Example
The example writes a user's two roles, customer and beta-tester, twice: the long way, with quotes and commas, and the short way, with %w. It prints the short one and checks that the two arrays are equal. Then it puts commas between the words inside %w, as people often do out of habit.
Prerequisites
- Ruby. This guide uses Ruby 4.0.10.
Step 1: Write the Code
Create a file named main.rb:

# The long way: quotes around each string and commas between them.
roles = ['customer', 'beta-tester']
# The short way: %w[] splits the words on spaces and makes each one a string.
same_roles = %w[customer beta-tester]
p same_roles
puts same_roles == roles
p prints a value the way Ruby writes it in code, so the strings appear in double quotes. == compares the two arrays item by item, and puts prints the result, true or false.
Step 2: Run It
ruby main.rb
["customer", "beta-tester"]
true
The command is the same in zsh, bash and PowerShell.
Step 3: Add Commas Between the Words
Replace main.rb with a version that has commas between the words, as in the long way:
roles = ['customer', 'beta-tester']
# Commas between the words, as in the long way.
same_roles = %w[customer, beta-tester]
p same_roles
puts same_roles == roles
ruby main.rb
["customer,", "beta-tester"]
false

What Happened
%w[customer beta-tester]made the same array as['customer', 'beta-tester']: two strings, so==printedtrue.- Each piece between the spaces became one string, exactly as written.
%wdoesn't treat a comma as a separator, so in%w[customer, beta-tester]the comma stayed at the end of the first string:"customer,". - The arrays were no longer equal, so
==printedfalse. Ruby gave no error or warning: the array is valid, it just holds a different string.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Commas between the words | The comma becomes part of the word: "customer," | Separate the words with spaces only |
Quotes around the words, as in %w['customer' 'beta-tester'] | The quotes are kept: ["'customer'", "'beta-tester'"] | Leave the quotes out |
Expecting numbers from %w[1 2 3] | You get strings, ["1", "2", "3"], and .sum stopped with String can't be coerced into Integer (TypeError) | Write numbers as an ordinary array: [1, 2, 3] |
Expecting #{...} to be filled in inside %w | %w[#{plan} free] kept the text #{plan} | Use %W |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
A string with a comma or quote in it, such as "customer," | Commas or quotes inside %w | Remove them: %w separates words with spaces only |
A name with a space, such as New York, comes out as two strings | The space separates words | Put a backslash before it: %w[New\ York Paris] gave ["New York", "Paris"] |
String can't be coerced into Integer (TypeError) | Arithmetic on strings that came from %w | Use an ordinary array of numbers |
FAQ
What's the difference between %w and %W?
%W fills in #{...} and escape sequences such as \n, and %w doesn't. With plan = 'pro', %w[#{plan} free] gave ["\#{plan}", "free"], while %W[#{plan} free] gave ["pro", "free"].
Can I make an array of symbols the same way?
Yes, with %i: %i[customer beta-tester] gave [:customer, :"beta-tester"].
Can the words go on several lines?
Yes. New lines separate words just like spaces do. In my check, a %w[...] with one word on each line, %w(customer beta-tester) and %w{customer beta-tester} all made the same array.
Conclusion
%w[...] is the short way to write an array of strings: words separated by spaces, with no quotes or commas. Anything else you type between the spaces, commas and quotes included, becomes part of a word, so leave them out, and use %W when you need #{...} filled in.
Additional Resources
- %w and %W: String-Array Literals and Percent Literals, in the Ruby documentation.
- Array, in the Ruby documentation.
