What's the best way of doing this:
mail = "[email protected]"
mail2 = mail.do_magic
# puts mail2 will return "[email protected]"
I'm thinking regex of course, but is there another cool way? If not, how should I do it using regexp?
What's the best way of doing this:
mail = "[email protected]"
mail2 = mail.do_magic
# puts mail2 will return "[email protected]"
I'm thinking regex of course, but is there another cool way? If not, how should I do it using regexp?
Not sure I completely understand what you're asking, but couldn't you use regex like this?
irb(main):001:0> email = "[email protected]"
=> "[email protected]"
irb(main):002:0> email.gsub(/@[\w.]+/, '@something.com')
=> "[email protected]"
Let me know I've missed something or if I'm not understanding the question correctly.
You can use regexps in strings indexes too:
email = "[email protected]"
replace = "foobar.invalid"
email[/@.*/] = "@#{replace}"
If you don't want to modify email
:
(new = email.dup)[/@.*/] = "@#{replace}"
p [email,new] # => ["[email protected]", "[email protected]"]
Another approach, avoiding regular expressions, is to split and join
new = [email.split('@').first, "foobar.invalid"].join('@')