开发者

Given a list of emails, how to determine if a email exists

开发者 https://www.devze.com 2023-02-18 16:20 出处:网络
Given a string like x@gmail.com, b@yahoo.com, c@ebay.com How can I, 开发者_运维百科using ruby, see if the string contains c@ebay.com ?emails = \"sam@sam.com, samuelgilman@samuelgilman.com, etc...\"

Given a string like x@gmail.com, b@yahoo.com, c@ebay.com

How can I, 开发者_运维百科using ruby, see if the string contains c@ebay.com ?


emails = "sam@sam.com, samuelgilman@samuelgilman.com, etc..."

emails.include?('sam@sam.com')
$ true 

emails.include?(s@s.com)
$false

update

If you want to pass multiple emails to include you can use the following code if you are using ruby 1.9.2:

emails.include?(email1 || email2)

As for ruby version below that you could do something like this:

emails = "sam@sam.com, samuelgilman@samuelgilman.com, etc..."
emails_to_check = [email1, email2]

emails_to_check.each do |email|  
    return true if emails.include(email)
end 

Since emails is a string it poses some problems. You can create an array or emails to check the string by with an each method but your code seems to be as good if not better.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号