Avoid using Perl-style special variables

이 페이지는 아직 한국어로 제공되지 않으며 번역 작업 중입니다. 번역에 관한 질문이나 의견이 있으시면 언제든지 저희에게 연락해 주십시오.

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:

require 'English'
puts $LAST_READ_LINE
puts $ERROR_INFO

This practice significantly enhances the readability of your code and makes it more accessible to developers who are not familiar with Perl-style variables.

Non-Compliant Code Examples

$! = ' -- '
$@ = ' -- '
$; = ' -- '
$, = ' -- '
$/ = ' -- '
$\ = ' -- '
$. = ' -- '
$_ = ' -- '
$> = ' -- '
$< = ' -- '
$$ = ' -- '
$~ = ' -- '
$* = ' -- '
$& = ' -- '

Compliant Code Examples

require "English"

$OUTPUT_FIELD_SEPARATOR = ' -- '
"Lorem ipsum dolor sit amet" =~ /dolor/
print $POSTMATCH, $PID, "\n"
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