이 페이지는 아직 한국어로 제공되지 않으며 번역 작업 중입니다. 번역에 관한 질문이나 의견이 있으시면 언제든지 저희에게 연락해 주십시오.

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: mervebolat/span-id-preprocessing