Prefer using `warn` over `$stderr.puts`

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

Metadata

ID: ruby-best-practices/use-warn

Language: Ruby

Severity: Notice

Category: Best Practices

Description

In Ruby, it is a good practice to use warn instead of $stderr.puts for issuing warning messages. The warn method is specifically designed for this purpose and its use makes the intention of the code clearer.

The importance of this rule lies in the fact that warn and $stderr.puts behave differently in certain situations. For example, warn will prepend the filename and line number to the warning message, which can be very helpful for debugging. Moreover, warn respects the -W command-line option for setting warning levels, while $stderr.puts does not.

To avoid violating this rule, replace any instances of $stderr.puts with warn when you want to issue a warning. Remember that the purpose of $stderr.puts is to write to the standard error, not to issue warnings. If you want to write to the standard error for reasons other than issuing warnings, $stderr.puts is the appropriate method to use.

Non-Compliant Code Examples

$stderr.puts 'foo bar baz'

Compliant Code Examples

warn 'foo bar baz'
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: antoine.dussault/service-representation-ga-docs-us1