Class: Hone::Patterns::SortLast
- Defined in:
- lib/hone/patterns/sort_last.rb
Overview
Pattern: array.sort.last -> array.max
sort.last sorts the entire array then takes the last element. max directly finds the maximum without creating intermediate array.
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
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/hone/patterns/sort_last.rb', line 13 def visit_call_node(node) super # Look for: .last or .last(n) where receiver is .sort return unless node.name == :last receiver = node.receiver return unless receiver.is_a?(Prism::CallNode) && receiver.name == :sort add_finding( node, message: "Use `.max` instead of `.sort.last` to avoid sorting entire array", speedup: "Avoids sorting entire array" ) end |