Remove "@" sign and everything after it in Ruby

Smoke picture Smoke · Aug 9, 2011 · Viewed 19.5k times · Source

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 ??

Answer

J Lundberg picture J Lundberg · Aug 9, 2011

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

email.split("@")[0]

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