class RuboCop::Cop::Performance::StringBytesize
“foobar”.bytesize
string_var.bytesize
# good
“foobar”.bytes.size
string_var.bytes.count
# bad
@example
responds to ‘#bytesize` method.
This cop is unsafe because it assumes that the receiver
@safety
avoiding the intermediate array allocation that `bytes.size` incurs.
The `bytesize` method is more efficient and directly returns the size in bytes,
Checks for calls to `#bytes` counting method and suggests using `bytesize` instead.
def on_send(node)
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