class RuboCop::Cop::Style::GlobalVars

Looks for uses of global variables. It does not report offenses for built-in global variables. Built-in global variables are allowed by default. Additionally users can allow additional variables via the AllowedVariables option.

Note that backreferences like $1, $2, etc are not global variables.

@example

# bad
$foo = 2
bar = $foo + 5

# good
FOO = 2
foo = 2
$stdin.read

Constants

BUILT_IN_VARS

built-in global variables and their English aliases www.zenspider.com/ruby/quickref.html

MSG

Public Instance Methods

allowed_var?(global_var) click to toggle source
# File lib/rubocop/cop/style/global_vars.rb, line 60
def allowed_var?(global_var)
  BUILT_IN_VARS.include?(global_var) || user_vars.include?(global_var)
end
check(node) click to toggle source
# File lib/rubocop/cop/style/global_vars.rb, line 72
def check(node)
  global_var, = *node

  add_offense(node.loc.name) unless allowed_var?(global_var)
end
on_gvar(node) click to toggle source
# File lib/rubocop/cop/style/global_vars.rb, line 64
def on_gvar(node)
  check(node)
end
on_gvasgn(node) click to toggle source
# File lib/rubocop/cop/style/global_vars.rb, line 68
def on_gvasgn(node)
  check(node)
end
user_vars() click to toggle source
# File lib/rubocop/cop/style/global_vars.rb, line 56
def user_vars
  cop_config['AllowedVariables'].map(&:to_sym)
end