Class: Hone::Patterns::Divmod
- Defined in:
- lib/hone/patterns/divmod.rb
Overview
Pattern: [n / d, n % d] -> n.divmod(d)
When you need both quotient and remainder, divmod computes them in a single operation instead of performing division twice.
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
Methods inherited from Base
#add_finding, inherited, #initialize, scan_file
Constructor Details
This class inherits a constructor from Hone::Patterns::Base
Instance Method Details
#visit_array_node(node) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/hone/patterns/divmod.rb', line 13 def visit_array_node(node) super elements = node.elements return unless elements.size == 2 first = elements[0] second = elements[1] return unless division_operation?(first) && modulo_operation?(second) return unless same_operands?(first, second) add_finding( node, message: "Use `.divmod` instead of `[n / d, n % d]` for single operation", speedup: "Single operation instead of two" ) end |