Omit the rb file extension in a require

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/no-explicit-rb-to-require

Language: Ruby

Severity: Notice

Category: Best Practices

Description

The rule “Omit the rb file extension in a require” is a coding convention in Ruby programming. It is designed to enforce the best practice of omitting the “.rb” extension when using the require or require_relative methods to include another Ruby file. This is because Ruby’s require method automatically searches for the “.rb” extension, and including it is redundant.

This rule is important because it promotes cleaner and more readable code. Writing the extension in a require statement is unnecessary and can lead to confusion. In addition, adhering to this rule can prevent potential issues with file loading, as Ruby might interpret a require with an explicit “.rb” extension differently.

To avoid violating this rule, omit the “.rb” extension when using require or require_relative. For example, instead of writing require 'foo.rb', write require 'foo'. For relative paths, instead of require_relative '../bar.rb', use require_relative '../bar'. This will ensure your code is clean, readable, and adheres to Ruby’s best practices.

Non-Compliant Code Examples

require 'foo.rb'
require_relative '../bar.rb'

Compliant Code Examples

require 'foo'
require 'bar.so'
require_relative '../baz'
require_relative '../qux.so'
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: may/unit-testing