Class: RuboCop::Cop::Performance::ArraySemiInfiniteRangeSlice
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
TargetRubyVersion,
AutoCorrector,
Base
|
|
Instance Chain:
self,
RangeHelp,
Base
|
|
Inherits: |
Base
|
Defined in: | lib/rubocop/cop/performance/array_semi_infinite_range_slice.rb |
Overview
Identifies places where slicing arrays with semi-infinite ranges
can be replaced by Array#take
and Array#drop
.
This cop was created due to a mistake in microbenchmark and hence is disabled by default.
Refer https://github.com/rubocop/rubocop-performance/pull/175#issuecomment-731892717
Constant Summary
-
MSG =
# File 'lib/rubocop/cop/performance/array_semi_infinite_range_slice.rb', line 36'Use `%<prefer>s` instead of `%<current>s` with semi-infinite range.'
-
RESTRICT_ON_SEND =
# File 'lib/rubocop/cop/performance/array_semi_infinite_range_slice.rb', line 39SLICE_METHODS
-
SLICE_METHODS =
# File 'lib/rubocop/cop/performance/array_semi_infinite_range_slice.rb', line 38Set[:[], :slice].freeze
Instance Method Summary
-
#on_csend(node)
Alias for #on_send.
- #on_send(node) (also: #on_csend)
- #correction(receiver, range_node) private
Instance Method Details
#correction(receiver, range_node) (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/array_semi_infinite_range_slice.rb', line 66
def correction(receiver, range_node) method_call = if range_node.begin "drop(#{range_node.begin.value})" elsif range_node.irange_type? "take(#{range_node.end.value + 1})" else "take(#{range_node.end.value})" end "#{receiver.source}.#{method_call}" end
#on_csend(node)
Alias for #on_send.
# File 'lib/rubocop/cop/performance/array_semi_infinite_range_slice.rb', line 62
alias on_csend on_send
#on_send(node) Also known as: #on_csend
[ GitHub ]# File 'lib/rubocop/cop/performance/array_semi_infinite_range_slice.rb', line 52
def on_send(node) endless_range_slice?(node) do |receiver, method_name, range_node| prefer = range_node.begin ? :drop : :take = format(MSG, prefer: prefer, current: method_name) add_offense(node, message: ) do |corrector| corrector.replace(node, correction(receiver, range_node)) end end end