When you execute /#{p}/ , this means that p will be interpreted as a regular expression. Since your p now \username , this compilation of Regexp will fail (since this is an invalid Unicode escape sequence):
>> Regexp.new "\\username" RegexpError: invalid Unicode escape: /\username/
those. execution /#{p}/ is equal to the entry /\username/ .
Therefore, you need to avoid p from any regular expressions so that it is correctly interpreted:
"Any String".match(/#{Regexp.escape(p)}/)
Or simply:
"Any String".match(Regexp.escape(p))
source share