liveBook · Manning

%w array constructor

As explained in The Well-Grounded Rubyist, Fourth Edition

The %w array constructor is Ruby syntax that creates an array of strings from whitespace-separated content, with %W enabling double-quoted interpolation rules.

The %w constructor creates an array of strings by splitting whitespace-separated content. Parentheses and braces can delimit the content, while escaped spaces keep words together. section 9.2.1

Basic construction

%w(Joe Leo III) produces ["Joe", "Leo", "III"]. The brace form, %w{ Joe Leo III }, produces the same array.

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Defines delimiters, whitespace splitting, escaped spaces, and interpolation, without detailing parser internals.
Examples
Uses names, a multiword name, and an interpolated age sentence to demonstrate construction and edge behavior.
What it assumes
Assumes familiarity with Ruby arrays and strings while demonstrating the constructor behavior through small interactive examples.
What you can do after
Apply it via Ruby syntax to write compact string-array literals and choose %W when interpolation is required.
How it connects
Connects array construction with strings, interpolation, and the related %i symbol-array notation.

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.