I have this line of code which Rubocop is complaining about
offset = -7
format('%.2i', offset)
Rubocop's message is
rubocop: warning C - Style/FormatStringToken: Prefer annotated tokens (like
%<foo>s
) over unannotated tokens (like%s
).
But even when reading the explanations in the documentation https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Style/FormatStringToken I cannot make it compliant.
The goal of the code is to add a 0 whenever the offset
is a single digit number and to not add anything when the number has two digits already.
I tried the following:
offset = -7
format('%.2{offset}', offset: offset)
To specify styles, you have to use <offset>
, not {offset}
. The correct syntax is:
format('%<offset>.2i', offset: offset)
#=> "-07"
The <variable_name>
part goes between %
and .2i
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With