Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Improper gsub replacement

Tags:

ruby

I am trying to replace all alphanumeric characters of an email with the '#' character using the gsub method but Ruby is inserting a backslash before the '@' character.

E.g:

"[email protected]".gsub(/[a-z0-9]/, "#") returns "###\#@###.###" instead of "####@###.###".

like image 365
Sofia Braun Avatar asked Aug 12 '16 15:08

Sofia Braun


1 Answers

It returns "####@###.###" as expected, try to:

puts "[email protected]".gsub(/[a-z0-9]/, "#")

What you see in IRB/Pry is the prevention of #@ being interpreted as string interpolation.

Please also refer to very valuable comment by @Stefan below.

like image 157
Aleksei Matiushkin Avatar answered Sep 22 '22 12:09

Aleksei Matiushkin