Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "Ambiguous regexp literal" in rubocop?

Tags:

rubocop

The following code

expect(foo).to match /#{MyGem.config.environment_name}/

triggers a rubocop issue

Warning: Ambiguous regexp literal. Parenthesize the method arguments if it's surely a regexp literal, or add a whitespace to the right of the / if it should be a division.

Can someone explain what the issue is and how to resolve it?

like image 473
CodeSmith Avatar asked Jul 12 '16 15:07

CodeSmith


2 Answers

Another way to fix this is to simply add parens as rubocop suggests. Change

expect(foo).to match /#{MyGem.config.environment_name}/

to

expect(foo).to match(/#{MyGem.config.environment_name}/)
like image 169
CodeSmith Avatar answered Nov 18 '22 08:11

CodeSmith


It is complaining that at this point:

match /#{MyGem.config.environment_name}/

it is unclear whether you're dividing two numbers or passing a RegExp literal to a method call.

In this particulare case, since you're just checking for the presence of a substring within a string, it would be better to use the RSpec #include matcher, like so:

expect(foo).to include MyGem.config.environment_name
like image 29
Drenmi Avatar answered Nov 18 '22 08:11

Drenmi