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-code-style/predicate-methods
Language: Ruby
Severity: Notice
Category: Code Style
Description
The rule ‘Use predicate methods over explicit comparisons with ==’ encourages the use of predicate methods in Ruby for cleaner, more idiomatic code. Predicate methods are methods that return a boolean value, and in Ruby, they are typically identified by the question mark at the end of the method name. These methods provide a more expressive and concise way to perform certain checks, such as checking if a number is even, odd, or nil.
This rule is important because it improves the readability and maintainability of the code. It reduces verbosity and makes the intention of the code clearer to other developers. Explicitly comparing values using == can sometimes lead to confusion or errors, especially with nil checks.
To avoid violations of this rule, use the built-in predicate methods provided by Ruby whenever possible. For instance, instead of checking if a number is even with num % 2 == 0, use the .even? method like so: num.even?. Similarly, instead of checking if a variable is nil with var == nil, use the .nil? method: var.nil?. By following these practices, you can write cleaner and more idiomatic Ruby code.