From 2c950b99e483652d6cea49b7710373882d243326 Mon Sep 17 00:00:00 2001 From: Phil Pirozhkov Date: Fri, 30 Dec 2022 17:14:12 +0300 Subject: [PATCH] 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. --- README.adoc | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/README.adoc b/README.adoc index 050b282f..54cf189c 100644 --- a/README.adoc +++ b/README.adoc @@ -4862,28 +4862,34 @@ sprintf('%d %d', 20, 10) # => '20 10' # good -sprintf('%d %d', first: 20, second: 10) +sprintf('%{first} %{second}', first: 20, second: 10) # => '20 10' format('%d %d', 20, 10) # => '20 10' # good -format('%d %d', first: 20, second: 10) +format('%{first} %{second}', first: 20, second: 10) # => '20 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, favor `%{name}` over `%s` or `%`. +An exception is when formatting, as the `%s` style uses format style, but `%{name}` style doesn't. [source,ruby] ---- # bad -format('Hello, %{name}', name: 'John') +format('Total: %d', sum: 123.567) +format('Hello, %', name: 'John') + +# ok - using format style +format('Total: %7.2f', sum: 123.456) # => ' 123.68' # good -format('Hello, %s', name: 'John') +format('Total: %{sum}d', sum: 123.567) +format('Hello, %{name}', name: 'John') ---- === Long Strings [[heredoc-long-strings]]