Programming
Ruby
Array
Coding Syntax
Software Development

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.

ruby
1colors = %w[red blue green]
2other_colors = ["red", "blue", "green"]
3
4p colors
5p other_colors

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:

ruby
1roles = %w[admin editor viewer]
2
3puts roles[0]
4puts roles.include?("editor")

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:

ruby
months = ["jan", "feb", "mar", "apr"]

You can write this:

ruby
months = %w[jan feb mar apr]

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.

ruby
names = %w[Alice Bob Carol]
p names

If you try this:

ruby
names = %w[Alice Smith Bob Jones]
p names

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:

ruby
names = %w[Alice\ Smith Bob\ Jones]
p names

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.

ruby
user = "alice"
values = %w[hello #{user}]
p values

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.

ruby
1user = "alice"
2
3plain = %w[hello #{user}]
4expanded = %W[hello #{user}]
5
6p plain
7p expanded

In that example:

  • '%w keeps #{user} as literal text'
  • '%W turns 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:

ruby
1languages = %w[ruby python go]
2more_languages = %w(ruby python go)
3extra_languages = %w|ruby python go|
4
5p languages
6p more_languages
7p extra_languages

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:

ruby
labels = ["New York", "San Francisco", "Los Angeles"]
p labels

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.
  • '%w is 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.

Course illustration
Course illustration

All Rights Reserved.