Posted 10 months ago. Visible to the public. Repeats. Linked content.

Ruby's percent notation can do more than strings

Percent Notation

We already know that that we can create strings using the percent notation:

%{<foo="bar's ton">} is perfectly fine Ruby.


But there is more. The curly brackets ({}) are interchangable with most unicode characters (e.g. square brackets[]).
Furthermore, you can add a "modifier" to the percent notation to control the return type of the expression:

%i Array of Symbols %q String %r Regular Expression %s Symbol %w Array of Strings %x Backtick (capture subshell result)*


%i[foo bar] => [:foo, :bar] %q[foo bar] => "foo bar"


To interpolate a dynamic value, swap the modifier with the corresponding uppercase letter:

%I[a list of #{'inter'}polated symbols] => [:a, :list, :of, :interpolated, :symbols]

*Please keep in mind that we'd prefer Open3 for shell commands.

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for old versions of Ruby on Rails (3.2 and 2.3).

Owner of this card:

Michael Leimstädtner
Last edit:
9 months ago
by Emanuel De
%, percent, string, notation, literal, regular, expression, quotes, cucumber, step, definition, regex, short
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Michael Leimstädtner to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more