Use helper functions to write files

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/file-write

Language: Ruby

Severity: Info

Category: Best Practices

Description

The rule emphasizes the importance of using helper functions such as File.write or File.binwrite to write files in Ruby. These helper functions are not only simpler and more readable, but they also handle resource management automatically, reducing the risk of file leaks caused by not closing files properly.

This rule is crucial as it promotes efficient memory usage and cleaner code. By using these helper functions, you’re ensuring that your files are closed after being written to, which prevents potential file and memory leaks in your application. This can be particularly important in larger applications, where such leaks could lead to significant performance issues.

To abide by this rule, always prefer File.write or File.binwrite over File.open when writing to a file. These methods open the file, write the content, and then close the file automatically. They also return the number of bytes written, which can be a useful piece of information. The code becomes more concise, more readable, and less prone to file leaks.

Non-Compliant Code Examples

File.open(filename, 'wb').write(content)
File.open(filename, 'wb') { |f| f.write(content) }
File.open(filename, 'wb') do |f|
  f.write(content)
end
File.open(filename, 'w').write(content)
File.open(filename, 'w') { |f| f.write(content) }
File.open(filename, 'w') do |f|
  f.write(content)
end

Compliant Code Examples

File.binwrite(filename, content)
File.write(filename, content)
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