Why does rubocop ask me to put // around the regex when I'm already using% r?

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 regex".

How can I get around this?

+5
source share
4 answers

I do not start rubocop, so I'm not sure if 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 
+1
source

You can disable (and enable) any rubocop cop by adding the .rubocop.yml file to the root of your project folder and setting up the appropriate configurations. To find out what you can do, check out the global default.yml in your rubocop package. He fully commented.

For this specific task, create .rubocop.yml and ...

To completely shut down your computer:

Style/RegexpLiteral: Enabled: false

Always use %r :

Style/RegexpLiteral: EnforcedStyle: percent_r

+3
source

You can use multi-line regular expression with /.../x either:

 regexp = / ((returned|undelivered) \s mail|mail \s delivery (\sfailed)?) /x 

More at Rubocop gem doc

+1
source

when I run rubocop on it, it complains that I need to "Use // around the regex".

How can I get around this?

I think the message is pretty clear: to get around it, you can use // around the regex:

 regexp = /((returned|undelivered)\smail|mail\sdelivery(\sfailed)?)/x 
-1
source

All Articles