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-case-equality
Language: Ruby
Severity: Notice
Category: Best Practices
Description
The case equality operator === in Ruby is used to test equality within a when clause of a case statement. However, it’s often considered a bad practice to use this operator explicitly outside of a case statement. This is because its behavior can be quite unpredictable and confusing, as it behaves differently for different classes.
The use of the === operator can lead to code that is harder to read and understand. It’s also potentially prone to bugs, as it might not behave as expected with certain objects. Therefore, it’s recommended to avoid the explicit use of the === operator.
Instead of using the === operator, it’s better to use more explicit methods that clearly indicate what you’re trying to achieve. For example, if you’re trying to check if a string matches a regular expression, you can use the match? method. If you want to check if an object is an instance of a certain class, you can use the is_a? method. These methods are much more clear and straightforward, leading to better, more maintainable code.