このページは日本語には対応しておりません。随時翻訳に取り組んでいます。翻訳に関してご質問やご意見ございましたら、お気軽にご連絡ください。

Metadata

ID: ruby-code-style/sprintf

Language: Ruby

Severity: Notice

Category: Best Practices

Description

The rule enforces the use of sprintf and format over the % operator for string formatting in Ruby. This is important because sprintf and format are more readable and less error-prone than the % operator.

The % operator can lead to confusion and bugs, especially when the array to be interpolated contains more items than expected or when it’s not clear what type of formatting is being applied.

To avoid this, always use sprintf or format for string formatting. These methods are more explicit about what formatting is being applied, making the code easier to understand and safer. For example, sprintf('%d %d', 1, 42) is preferred over '%d %d' % [1, 42].

Non-Compliant Code Examples

'%d %d' % [1, 42]

Compliant Code Examples

sprintf('%d %d', 1, 42)
sprintf('%<foo>d %<bar>d', foo: 1, bar: 42)

format('%d %d', 1, 42)
format('%<foo>d %<bar>d', foo: 1, bar: 42)
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis

PREVIEWING: rtrieu/product-analytics-ui-changes