liveBook · Manning

%i array constructor

As explained in The Well-Grounded Rubyist, Fourth Edition

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

Use %i with a delimiter to create an array of symbols from whitespace-separated content: %i(a b c) produces [:a, :b, :c]. section 9.2.1

Contrast the related forms:

  • %i creates symbols without interpolation. section 9.2.1
  • %I enables double-quoted interpolation rules, as in %I("#{d}"). section 9.2.1
  • %w and %W provide corresponding whitespace-separated string arrays. section 9.2.1
>> %i(a b c)
=> [:a, :b, :c]
>> d = "David"
=> "David"
>> %I("#{d}")
=> [:"\"David\""]

Compare ordinary symbol construction with interpolated %I syntax and inspect the resulting arrays. section 9.2.1

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Defines %i and contrasts %I interpolation, but does not detail delimiter escaping or broader symbol semantics.
Examples
Demonstrates %i(a b c), interpolated %I content, and corresponding %w and %W forms.
What you can do after
Build symbol arrays with %i and choose %I when interpolation rules are required.
How it connects
Connects symbol arrays with array constructors, string arrays, and string interpolation.

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.