Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ruby

rubocop

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?

like image 402
kchoi Avatar asked Jun 21 '16 22:06

kchoi


2 Answers

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

For this particular problem, create a .rubocop.yml and...

To disable the cop completely:

Style/RegexpLiteral: Enabled: false

To always use %r:

Style/RegexpLiteral: EnforcedStyle: percent_r

like image 121
R. Peereboom Avatar answered Nov 13 '22 05:11

R. Peereboom


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
like image 23
seph Avatar answered Nov 13 '22 06:11

seph