Class: Hone::Patterns::EachWithIndex
- Defined in:
- lib/hone/patterns/each_with_index.rb
Overview
Pattern: array.each_with_index { |item, i| ... } when index or element unused
When using each_with_index but only using the element (not the index), use plain .each instead to avoid index tracking overhead.
When using each_with_index but only using the index (not the element), use array.size.times { |i| ... } instead for clarity and efficiency.
Examples:
# Bad: index `i` is never used
array.each_with_index { |item, i| puts item }
# Good: use plain each
array.each { |item| puts item }
# Bad: element `item` is never used
array.each_with_index { |item, i| puts i }
# Good: use times
array.size.times { |i| puts i }
Defined Under Namespace
Classes: LocalVariableCollector
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_call_node(node) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/hone/patterns/each_with_index.rb', line 29 def visit_call_node(node) super return unless node.name == :each_with_index && node.block.is_a?(Prism::BlockNode) check_block_parameter_usage(node) end |