Skip to content

constant-definition-in-block

Rubysuspiciousno fixdefault: offruby/rubocop

Reports a constant, class or module defined directly in a block. enums blocks (Sorbet) are not reported.

A constant in a block belongs to the scope that encloses the block, not to the block, so the definition is global to the file or class, and runs again each time the block does, with a warning about a constant that is already defined.

To fix, define the constant outside the block, or use a variable or let.

Reported
...
end
end
[1].each { X = 1 }
ruby:
lint:
rules:
constant-definition-in-block: warn # off (default), warn, error

Enabled by the presets: ruby/rubocop.

Suppress one line with sudsy-disable-next-line constant-definition-in-block in a comment of the file’s language.

The same check in other tools; Sudsy reports what each upstream rule reports.

Tool Rule Match
rubocop Lint/ConstantDefinitionInBlock full