Class: Hone::Patterns::RedundantStringChars
- Defined in:
- lib/hone/patterns/redundant_string_chars.rb
Overview
Detects str.chars[i] which should be str[i]
str.chars creates an array of single-character strings, then indexes it. Direct string indexing is much faster.
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
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/hone/patterns/redundant_string_chars.rb', line 24 def visit_call_node(node) if chained_chars_index?(node) replacement = suggest_replacement(node) add_finding( node, message: "Use `#{replacement}` instead of `chars[...]` to avoid array allocation", speedup: "Significant - avoids creating array of all characters" ) elsif chained_chars_first_last?(node) replacement = suggest_first_last_replacement(node) add_finding( node, message: "Use `#{replacement}` instead of `chars.#{node.name}` to avoid array allocation", speedup: "Significant - avoids creating array of all characters" ) end super end |