Prefer sprintf and form

Cette page n'est pas encore disponible en français, sa traduction est en cours.
Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.

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