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

This page is not yet available in Spanish. We are working on its translation.
If you have any questions or feedback about our current translation project, feel free to reach out to us!

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: rtrieu/product-analytics-ui-changes