Class: Hone::Patterns::CharsMapOrd
- Defined in:
- lib/hone/patterns/chars_map_ord.rb
Overview
Pattern: str.chars.map(&:ord) -> str.codepoints
The .chars.map(&:ord) chain creates an intermediate array of single-char strings, then maps each to its ordinal. Using .codepoints directly is faster and allocates less memory.
From sqids-ruby commit 9413b68
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
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/hone/patterns/chars_map_ord.rb', line 16 def visit_call_node(node) super # Look for: .map(&:ord) where receiver is .chars return unless node.name == :map && block_arg_is_symbol?(node, :ord) receiver = node.receiver return unless receiver.is_a?(Prism::CallNode) && receiver.name == :chars add_finding( node, message: "Use `.codepoints` instead of `.chars.map(&:ord)` to avoid intermediate array allocation", speedup: "Fewer allocations" ) end |