Skip to content

CAT 039

unurgunite edited this page Jul 11, 2026 · 3 revisions

CAT-039 — Regex.new in method → constant

Severity: warningConfidence: highAuto-fix:

Description

Regex.new in method → constant — Creating a Regex inside a method recompiles on each call. Extract to a constant.

Bad code

def parse(str)
  regex = /pattern/
  regex.match(str)
end

Performance Impact

Tip

This rule can significantly improve performance in hot code paths.

Benchmark

Note

See bench results at bench/cat-039/

Clone this wiki locally