Class: Rubocop::Cop::Style::NumericLiterals

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/style/numeric_literals.rb

Overview

This cop checks for big numeric literals without _ between groups of digits in them.

Constant Summary collapse

MSG =
'Add underscores to large numeric literals to ' +
'improve their readability.'

Instance Attribute Summary

Attributes inherited from Cop

#autocorrect, #corrections, #debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, all, #autocorrect_action, cop_name, cop_type, #do_autocorrect, #ignore_node, inherited, #initialize, lint?, #name, rails?, style?

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Instance Method Details

#on_int(node) ⇒ Object Also known as: on_float



12
13
14
15
16
17
18
19
# File 'lib/rubocop/cop/style/numeric_literals.rb', line 12

def on_int(node)
  value, = *node

  if value > 10000 &&
      node.loc.expression.source.split('.').grep(/\d{6}/).any?
    add_offence(:convention, node.loc.expression, MSG)
  end
end