Skip to content

CAT 019

unurgunite edited this page Jul 11, 2026 · 3 revisions

CAT-019 — Regex.new in loop → constant

Severity: warning | Confidence: high | Auto-fix:

Tip

This rule supports auto-fix via --fix

Description

Regex.new in loop → constant — Creating a Regex inside a loop recompiles the pattern each time. Extract to a constant.

Bad code

3.times { Regex.new("pattern").match(str) }

Good code

PATTERN = /pattern/
3.times { PATTERN.match(str) }

Performance Impact

Tip

This rule can significantly improve performance in hot code paths.

Benchmark

Note

See bench results at bench/cat-019/

Clone this wiki locally