0

So I am trying to match email to email domains (ie bob@test to @test.com). Both the array of emails and email domains are in separate arrays. Right now I am trying the following:

@Domains.each do |domain|
    if @Emails.include?(/.*/ + domain) then 
        #do stuff
    end
end

That is obviously throwing errors and I cannot find another solution. Any ideas?

2

3 Answers 3

2

It is not clear what you are trying to do with a regex. This is a minimum fix to make it run. It does not use a regex.

@Domains.each do |domain|
  if @Emails.any?{|email| email.end_with?(domain)} 
    #do stuff
  end
end
Sign up to request clarification or add additional context in comments.

1 Comment

I was trying to get it to match *@domain. I will try you suggestion, but it doesn't start with the domain, it ends with it.
0

You can use each_with_object with a hash, grep the @emails array for matching patterns and associate both:

@domains.each_with_object Hash.new do |domain, matches|
  matches[domain] = @emails.grep domain
end

Comments

0
@domains = ["hotmail.com", "gmail.com"]
@emails = ["[email protected]", "[email protected]", "[email protected]"]
@a = []

@domains.each {|d|
  @a << @emails.select{ |e| e=~ /#{d}/ }
}

@a.flatten! #=> ["[email protected]", "[email protected]"]

Now I can do stuff with @a

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.