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
-
CORRECTION_WITHOUT_ARGUMENTS =
# File 'lib/rubocop/cop/correctors/each_to_for_corrector.rb', line 10'for _ in %<enumerable>s do'
-
CORRECTION_WITH_ARGUMENTS =
# File 'lib/rubocop/cop/correctors/each_to_for_corrector.rb', line 9'for %<variables>s in %<collection>s do'
Class Method Summary
- .new(block_node) ⇒ EachToForCorrector constructor
Instance Attribute Summary
- #argument_node readonly private
- #block_node readonly private
- #collection_node readonly private
Instance Method Summary
- #call(corrector)
- #correction private
- #offending_range private
Constructor Details
.new(block_node) ⇒ EachToForCorrector
# 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