Search code examples
ruby-on-railsruby

Hide part of an email address


What's the best way to hide 4 characters before the @ sign of an email address using ruby eg

[email protected] = fake####@example.com 

It's going to be used in a view when I display a list of testimonials and I don't want to display the whole address.

My long way round attempt:

name = '[email protected]'.split("@")[0]
email = '[email protected]'.split("@")[1]
new_address = name [0..-4] + "@" + email

Solution

  • Try the below that will even handle short names like [email protected]

    '[email protected]'.gsub(/.{0,4}@/, '####@')