jonepatr
jonepatr

Reputation: 7809

How to get domain from email

How do I get the domain from an email-adress in ruby?

Upvotes: 42

Views: 18840

Answers (2)

miku
miku

Reputation: 188054

Given the invariant that your input string is a valid email address, you could just write:

>> "[email protected]".split("@").last
=> "mycorp.com"

Upvotes: 84

noodl
noodl

Reputation: 17408

If you prefer using a library dedicated to understanding these things:

→ irb -rmail
ruby-1.9.2-p0 > Mail::Address.new('[email protected]').domain
 => "example.com" 

Upvotes: 51

Related Questions