class RuboCop::Cop::Style::RedundantArrayConstructor
Checks for the instantiation of array using redundant ‘Array` constructor. Autocorrect replaces to array literal which is the simplest and fastest.
@example
# bad Array.new([]) Array[] Array([]) Array.new(['foo', 'foo', 'foo']) Array['foo', 'foo', 'foo'] Array(['foo', 'foo', 'foo']) # good [] ['foo', 'foo', 'foo'] Array.new(3, 'foo') Array.new(3) { 'foo' }
Constants
- MSG
- RESTRICT_ON_SEND
Public Instance Methods
on_send(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_array_constructor.rb, line 47 def on_send(node) return unless (array_literal = redundant_array_constructor(node)) receiver = node.receiver selector = node.loc.selector if node.method?(:new) range = receiver.source_range.join(selector) replacement = array_literal elsif node.method?(:Array) range = selector replacement = array_literal else range = receiver replacement = selector.begin.join(node.source_range.end) end register_offense(range, node, replacement) end
Private Instance Methods
register_offense(range, node, replacement)
click to toggle source
# File lib/rubocop/cop/style/redundant_array_constructor.rb, line 69 def register_offense(range, node, replacement) add_offense(range) do |corrector| corrector.replace(node, replacement.source) end end