class RuboCop::Cop::Naming::VariableNumber

Makes sure that all numbered variables use the configured style, snake_case, normalcase, or non_integer, for their numbering.

Additionally, ‘CheckMethodNames` and `CheckSymbols` configuration options can be used to specify whether method names and symbols should be checked. Both are enabled by default.

@example EnforcedStyle: normalcase (default)

# bad
:some_sym_1
variable_1 = 1

def some_method_1; end

def some_method1(arg_1); end

# good
:some_sym1
variable1 = 1

def some_method1; end

def some_method1(arg1); end

@example EnforcedStyle: snake_case

# bad
:some_sym1
variable1 = 1

def some_method1; end

def some_method_1(arg1); end

# good
:some_sym_1
variable_1 = 1

def some_method_1; end

def some_method_1(arg_1); end

@example EnforcedStyle: non_integer

# bad
:some_sym1
:some_sym_1

variable1 = 1
variable_1 = 1

def some_method1; end

def some_method_1; end

def some_methodone(arg1); end
def some_methodone(arg_1); end

# good
:some_symone
:some_sym_one

variableone = 1
variable_one = 1

def some_methodone; end

def some_method_one; end

def some_methodone(argone); end
def some_methodone(arg_one); end

# In the following examples, we assume `EnforcedStyle: normalcase` (default).

@example CheckMethodNames: true (default)

# bad
def some_method_1; end

@example CheckMethodNames: false

# good
def some_method_1; end

@example CheckSymbols: true (default)

# bad
:some_sym_1

@example CheckSymbols: false

# good
:some_sym_1

@example AllowedIdentifiers: [capture3]

# good
expect(Open3).to receive(:capture3)

@example AllowedPatterns: [‘_vd+z’]

# good
:some_sym_v1

Constants

MSG

Public Instance Methods

on_arg(node) click to toggle source
# File lib/rubocop/cop/naming/variable_number.rb, line 114
def on_arg(node)
  @node = node
  name, = *node
  return if allowed_identifier?(name)

  check_name(node, name, node.loc.name)
end
Also aliased as: on_lvasgn, on_ivasgn, on_cvasgn, on_gvasgn
on_cvasgn(node)
Alias for: on_arg
on_def(node) click to toggle source
# File lib/rubocop/cop/naming/variable_number.rb, line 126
def on_def(node)
  @node = node
  return if allowed_identifier?(node.method_name)

  check_name(node, node.method_name, node.loc.name) if cop_config['CheckMethodNames']
end
Also aliased as: on_defs
on_defs(node)
Alias for: on_def
on_gvasgn(node)
Alias for: on_arg
on_ivasgn(node)
Alias for: on_arg
on_lvasgn(node)
Alias for: on_arg
on_sym(node) click to toggle source
# File lib/rubocop/cop/naming/variable_number.rb, line 134
def on_sym(node)
  @node = node
  return if allowed_identifier?(node.value)

  check_name(node, node.value, node) if cop_config['CheckSymbols']
end
valid_name?(node, name, given_style = style) click to toggle source
Calls superclass method
# File lib/rubocop/cop/naming/variable_number.rb, line 110
def valid_name?(node, name, given_style = style)
  super || matches_allowed_pattern?(name)
end

Private Instance Methods

message(style) click to toggle source
# File lib/rubocop/cop/naming/variable_number.rb, line 143
def message(style)
  identifier_type =
    case @node.type
    when :def, :defs then 'method name'
    when :sym        then 'symbol'
    else                  'variable'
    end

  format(MSG, style: style, identifier_type: identifier_type)
end