Skip to main content

How to Write an Array of Strings in Ruby with the %w Percent Literal

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
%w[<word> <word> ...]
PartWhat it does
%wStarts 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:

main.rb, which writes the roles customer and beta-tester once with quotes and commas and once with %w, then prints the %w array with p and whether the two arrays are equal with puts.

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​

Terminal
ruby main.rb
Output
["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:

main.rb
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
Terminal
ruby main.rb
Output
["customer,", "beta-tester"]
false

A zsh terminal where ruby main.rb prints the strings customer and beta-tester, then true; with commas inside %w, it prints the strings customer with a comma and beta-tester, then false.

What Happened​

  • %w[customer beta-tester] made the same array as ['customer', 'beta-tester']: two strings, so == printed true.
  • Each piece between the spaces became one string, exactly as written. %w doesn'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 == printed false. Ruby gave no error or warning: the array is valid, it just holds a different string.

Common Mistakes​

MistakeWhat happensDo this instead
Commas between the wordsThe 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​

SymptomLikely causeFix
A string with a comma or quote in it, such as "customer,"Commas or quotes inside %wRemove them: %w separates words with spaces only
A name with a space, such as New York, comes out as two stringsThe space separates wordsPut 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 %wUse 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​