class RuboCop::Cop::Style::ItAssignment
Checks for assignments to a local ‘it` variable inside a block where `it` can refer to the first anonymous parameter as of Ruby 3.4.
Although Ruby allows reassigning ‘it` in these cases, it could cause confusion if `it` is used as a block parameter elsewhere. For
consistency, this also applies to numblocks and blocks with parameters, even though `it` cannot be used in those cases.
@example
# bad foo { it = 5 } foo { |bar| it = bar } foo { it = _2 } # good - use a different variable name foo { var = 5 } foo { |bar| var = bar } foo { bar = _2 }
Constants
- MSG
Public Instance Methods
Source
# File lib/rubocop/cop/style/it_assignment.rb, line 27 def on_lvasgn(node) return unless node.name == :it return unless node.each_ancestor(:any_block).any? add_offense(node.loc.name) end