Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove "@" sign and everything after it in Ruby

I am working on an application where I need to pass on the anything before "@" sign from the user's email address as his/her first name and last name. For example if the user has an email address "[email protected]" than when the user submits the form I remove "@example.com" from the email and assign "user" as the first and last name.

I have done research but was not able to find a way of doing this in Ruby. Any suggestions ??

like image 465
Smoke Avatar asked Aug 09 '11 19:08

Smoke


2 Answers

You can split on "@" and just use the first part.

email.split("@")[0]

That will give you the first part before the "@".

like image 58
J Lundberg Avatar answered Nov 09 '22 10:11

J Lundberg


To catch anything before the @ sign:

my_string = "[email protected]"
substring = my_string[/[^@]+/]
# => "user"
like image 48
Dylan Markow Avatar answered Nov 09 '22 09:11

Dylan Markow