123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::EachToForCorrector

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Macros
Inherits: Object
Defined in: lib/rubocop/cop/correctors/each_to_for_corrector.rb

Overview

This class autocorrects #each enumeration to for iteration.

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(block_node) ⇒ EachToForCorrector

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/each_to_for_corrector.rb', line 12

def initialize(block_node)
  @block_node = block_node
  @collection_node = block_node.receiver
  @argument_node = block_node.arguments
end

Instance Attribute Details

#argument_node (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/each_to_for_corrector.rb', line 24

attr_reader :block_node, :collection_node, :argument_node

#block_node (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/each_to_for_corrector.rb', line 24

attr_reader :block_node, :collection_node, :argument_node

#collection_node (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/each_to_for_corrector.rb', line 24

attr_reader :block_node, :collection_node, :argument_node

Instance Method Details

#call(corrector)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/each_to_for_corrector.rb', line 18

def call(corrector)
  corrector.replace(offending_range, correction)
end

#correction (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/each_to_for_corrector.rb', line 26

def correction
  if block_node.arguments?
    format(CORRECTION_WITH_ARGUMENTS,
           collection: collection_node.source,
           variables: argument_node.children.first.source)
  else
    format(CORRECTION_WITHOUT_ARGUMENTS, enumerable: collection_node.source)
  end
end

#offending_range (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/each_to_for_corrector.rb', line 36

def offending_range
  begin_range = block_node.source_range.begin

  if block_node.arguments?
    begin_range.join(argument_node.source_range.end)
  else
    begin_range.join(block_node.loc.begin.end)
  end
end