I am trying to replace all alphanumeric characters of a letter with the character “#” using the gsub method, but Ruby inserts a backslash before the “@” character.
gsub
eg:
" john@doe.com ".gsub(/[a-z0-9]/, "#") returns "###\#@###.###" instead of "####@###.###" .
" john@doe.com ".gsub(/[a-z0-9]/, "#")
"###\#@###.###"
"####@###.###"
It returns "####@###.###" , as expected, to try:
puts " john@doe.com ".gsub(/[a-z0-9]/, "#")
What you see in IRB / Pry is to prevent #@ interpreting as string interpolation.
#@
Please refer to @Stefan's very valuable comment below.
tr faster than gsub :
tr
puts " foo@bar.com ".tr('a-zA-Z0-9', '#') # >> ###@###.###