Class: RuboCop::Cop::Performance::TimesMap

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/performance/times_map.rb

Overview

Checks for .times.map calls. In most cases such calls can be replaced with an explicit array creation.

Examples:

# bad
9.times.map do |i|
  i.to_s
end

# good
Array.new(9) do |i|
  i.to_s
end

Constant Summary collapse

MESSAGE =
'Use `Array.new(%<count>s)` with a block instead of `.times.%<map_or_collect>s`'
MESSAGE_ONLY_IF =
'only if `%<count>s` is always 0 or more'
RESTRICT_ON_SEND =
%i[map collect].freeze

Instance Method Summary collapse

Instance Method Details

#on_block(node) ⇒ Object Also known as: on_numblock



44
45
46
# File 'lib/rubocop/cop/performance/times_map.rb', line 44

def on_block(node)
  check(node)
end

#on_send(node) ⇒ Object Also known as: on_csend



39
40
41
# File 'lib/rubocop/cop/performance/times_map.rb', line 39

def on_send(node)
  check(node)
end