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-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'
Seamless integrations. Try Datadog Code Analysis
Datadog Code Analysis
Try this rule and analyze your code with Datadog Code Analysis
How to use this rule
1
2
rulesets:- ruby-best-practices # Rules to enforce Ruby best practices.
Create a static-analysis.datadog.yml with the content above at the root of your repository
Use our free IDE Plugins or add Code Analysis scans to your CI pipelines