From d7a2e5298fd33f1ba3cab5329fb3c828affd5aef Mon Sep 17 00:00:00 2001 From: Phil Pirozhkov Date: Fri, 30 Dec 2022 17:14:12 +0300 Subject: [PATCH 1/2] Change the suggested format token style According to [research](https://github.com/rubocop/rubocop/issues/8827#issuecomment-1159313300): annotated (`%s`): 57497 files inspected, 5785 offenses detected template (`%{name}`): 57497 files inspected, 1279 offenses detected unannotated (`%s`) 57497 files inspected, 6424 offenses detected the `%{name}` is the most commonly used style. The annotated is widely used and acceptable as well. --- README.adoc | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/README.adoc b/README.adoc index cb5b0333..91525189 100644 --- a/README.adoc +++ b/README.adoc @@ -5264,14 +5264,7 @@ Prefer the use of `sprintf` and its alias `format` over the fairly cryptic `Stri # => '20 10' # good -sprintf('%d %d', 20, 10) -# => '20 10' - -# good -sprintf('%d %d', first: 20, second: 10) -# => '20 10' - -format('%d %d', 20, 10) +sprintf('%{first} %{second}', first: 20, second: 10) # => '20 10' # good @@ -5281,15 +5274,23 @@ format('%d %d', first: 20, second: 10) === Named Format Tokens [[named-format-tokens]] -When using named format string tokens, favor `%s` over `%{name}` because it encodes information about the type of the value. +When using named format string tokens, two styles are considered acceptable - `%{name}` and `%s`. +The second variant encodes information about the type of the value, and it's the only one that accepts a format specifier, so reach for it when you need to control the type, the width or the precision. [source,ruby] ---- # bad -format('Hello, %{name}', name: 'John') +format('%s is %d years old', 'John', 42) + +# good +format('%{name} is %{age} years old', name: 'John', age: 42) # good -format('Hello, %s', name: 'John') +format('%s is %d years old', name: 'John', age: 42) + +# good - the format specifier rounds the value and pads it to the given width +format('Total: %7.2f', sum: 123.456) +# => 'Total: 123.46' ---- === Long Strings [[heredoc-long-strings]] From 355853a9d4c0ffac000091355ddf078fa0a882c0 Mon Sep 17 00:00:00 2001 From: Phil Pirozhkov Date: Tue, 6 Oct 2026 00:33:21 +0300 Subject: [PATCH 2/2] Clarify description of named format string tokens Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- README.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.adoc b/README.adoc index 91525189..ae7e41ea 100644 --- a/README.adoc +++ b/README.adoc @@ -5275,7 +5275,7 @@ format('%d %d', first: 20, second: 10) === Named Format Tokens [[named-format-tokens]] When using named format string tokens, two styles are considered acceptable - `%{name}` and `%s`. -The second variant encodes information about the type of the value, and it's the only one that accepts a format specifier, so reach for it when you need to control the type, the width or the precision. +The second variant specifies a conversion type and is the only one that accepts flags, width or precision, so reach for it when you need those formatting controls. [source,ruby] ----