class RuboCop::Cop::Performance::Size

This cop is used to identify usages of `count` on an `Array` and `Hash` and change them to `size`.

@example

# bad
[1, 2, 3].count

# bad
{a: 1, b: 2, c: 3}.count

# good
[1, 2, 3].size

# good
{a: 1, b: 2, c: 3}.size

# good
[1, 2, 3].count { |e| e > 2 }

TODO: Add advanced detection of variables that could have been assigned to an array or a hash.

Constants

MSG

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/performance/size.rb, line 41
def autocorrect(node)
  ->(corrector) { corrector.replace(node.loc.selector, 'size') }
end
on_send(node) click to toggle source
# File lib/rubocop/cop/performance/size.rb, line 29
def on_send(node)
  receiver, method, args = *node

  return if receiver.nil?
  return unless method == :count
  return unless array?(receiver) || hash?(receiver)
  return if node.parent && node.parent.block_type?
  return if args

  add_offense(node, node.loc.selector)
end

Private Instance Methods

array?(node) click to toggle source
# File lib/rubocop/cop/performance/size.rb, line 47
def array?(node)
  receiver, method = *node
  _, constant = *receiver

  node.array_type? || constant == :Array || method == :to_a
end
hash?(node) click to toggle source
# File lib/rubocop/cop/performance/size.rb, line 54
def hash?(node)
  receiver, method = *node
  _, constant = *receiver

  node.hash_type? || constant == :Hash || method == :to_h
end