LoginSignup
2
3

More than 5 years have passed since last update.

RuboCop | Style/GlobalVars

Last updated at Posted at 2014-07-03

RuboCop | Style/GlobalVars

概要

RuboCopの「Style/GlobalVars」警告について。

組み込み以外の自分で定義したグローバル変数に対して警告を出します。
例外として警告対象外にする変数名を配列で指定することが可能です。
デフォルトは除外対象なしです。

GlobalVars

各設定値での検証結果をまとめます。

検証プログラム

global_vars.rb

print $HOGE
print $HIGE
print $HAGE
print $ARGV # => 組み込みは警告対象外

実行結果 デフォルト の場合

※明示的に設定しているが、デフォルト値なので何も設定しなくてもよい

.rubocop.yml
GlobalVars:
  AllowedVariables: []
$ rubocop global_vars.rb
Inspecting 1 file
C

Offenses:

global_vars.rb:1:7: C: Do not introduce global variables.
print $HOGE
      ^^^^^
global_vars.rb:2:7: C: Do not introduce global variables.
print $HIGE
      ^^^^^
global_vars.rb:3:7: C: Do not introduce global variables.
print $HAGE
      ^^^^^

1 file inspected, 3 offenses detected

実行結果 $HOGE , $HAGE を警告対象外にする場合 の場合

.rubocop.yml
GlobalVars:
  AllowedVariables: [$HOGE, $HAGE]
$ rubocop global_vars.rb
Inspecting 1 file
C

Offenses:

global_vars.rb:2:7: C: Do not introduce global variables.
print $HIGE
      ^^^^^

1 file inspected, 1 offense detected
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3