-
-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Performance/ConstantRegexp with a constant defined in a subclass #205
Comments
It would be more efficient to cache the result. One "correct" code should probably be: class C
def self.regexp
@regexp ||= %r{foo #{self::BASE_PATTERN}}
end
end Is that accepted by by ConstantRegexp? Although this won't work from Ractors... (one should use my Or maybe this would work? def self.inherited(base)
# Use the `o` flag because `BASE_PATTERN` isn't defined yet
base.class_eval "REGEXP = /foo #{self::BASE_PATTERN}/o"
end |
I like this and changed my code to use this pattern, but it is still not accepted by the cop, no. |
I think that memoized regexp like that should be acceptable 👍 |
[Fix #205] Update `Performance/ConstantRegexp` to allow memoized regexps
When a regexp depends on a constant defined in a subclass, neither of the fixes suggested by
Performance/ConstantRegexp
work:If the pattern is change to use the
/o
modifier, the first time the method is triggered the regexp will be interpolated for both subclasses:If the regexp is extracted to a constant, it needs to be duplicated in every subclass (and the regexp could be considerably more complicated than the example here).
RuboCop version
Include the output of
rubocop -V
orbundle exec rubocop -V
if using Bundler. Here's an example:The text was updated successfully, but these errors were encountered: