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/no-cryptic-perlisms
Language: Ruby
Severity: Notice
Category: Code Style
Description
The rule ‘Avoid using Perl-style special variables’ is important for improving the readability and maintainability of your code. Perl-style special variables, such as $0, $1, and $_, while powerful, can make your code less readable and harder to understand, especially for developers unfamiliar with Perl or its influence on Ruby. They can also introduce subtle bugs due to their global nature and the special behavior associated with them.
To avoid violating this rule, you can use the more descriptive aliases provided by the English library. This library, which is part of Ruby’s standard library, provides human-readable names for Perl-style special variables. For example, instead of using $& to get the string matched by the last successful pattern match, you can use $MATCH.
Here’s a compliant code example: Instead of $_, you can use $LAST_READ_LINE. Instead of $!, use $ERROR_INFO. This makes your code more self-explanatory and reduces the potential for confusion. Example:
This practice significantly enhances the readability of your code and makes it more accessible to developers who are not familiar with Perl-style variables.