kchoi kchoi - 1 month ago 8
Ruby Question

Why is rubocop asking me to put // around regex when i'm using %r already?

I have the following regex

regexp = %r{
((returned|undelivered)\smail|mail\sdelivery(\sfailed)?)
}x


But when I run rubocop on it, it complains that I need to "Use // around regular expression."

How can I get around it?

Answer

I don't run rubocop so not sure this will solve your problem. You can use // instead of {} to surround the regex when using %r:

regexp = %r/((returned|undelivered)\smail|mail\sdelivery(\sfailed)?)/x