Class: RuboCop::Cop::Performance::StringBytesize
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
AutoCorrector,
Base
|
|
Instance Chain:
self,
Base
|
|
Inherits: |
Base
|
Defined in: | lib/rubocop/cop/performance/string_bytesize.rb |
Overview
Checks for calls to #bytes
counting method and suggests using bytesize
instead.
The bytesize
method is more efficient and directly returns the size in bytes,
avoiding the intermediate array allocation that bytes.size
incurs.
Constant Summary
-
MSG =
# File 'lib/rubocop/cop/performance/string_bytesize.rb', line 25'Use `String#bytesize` instead of calculating the size of the bytes array.'
-
RESTRICT_ON_SEND =
# File 'lib/rubocop/cop/performance/string_bytesize.rb', line 26%i[size length count].freeze
Instance Method Summary
-
#on_csend(node)
Alias for #on_send.
- #on_send(node) (also: #on_csend)
Instance Method Details
#on_csend(node)
Alias for #on_send.
# File 'lib/rubocop/cop/performance/string_bytesize.rb', line 41
alias on_csend on_send
#on_send(node) Also known as: #on_csend
[ GitHub ]# File 'lib/rubocop/cop/performance/string_bytesize.rb', line 32
def on_send(node) string_bytes_method?(node) do range = node.receiver.loc.selector.begin.join(node.source_range.end) add_offense(range) do |corrector| corrector.replace(range, 'bytesize') end end end