AnApprentice
AnApprentice

Reputation: 111040

Given a list of domains, how to match if a value is contained?

I have the following:

EXCLUSION_DOMAINS = %w[
  example.com
  test.com
  asdf.com
  yahoo.
  gmail
]

Some of these exclusion domains have the full address like.com, others just a high level domain for generic matching.

Given this list, and given a value, how can I determine if the value matches one of the exclusion domains w rails 3?

Thanks

Upvotes: 0

Views: 69

Answers (3)

KMC
KMC

Reputation: 20036

I think you can simply use string.split(".").first
Even if there is not "." (e.g. gmail instead of gmail.) you will still get the "gmail"

Upvotes: 0

m3nd3s
m3nd3s

Reputation: 21

I solve this problem by using that:

my_domain = "gmail.com.br"
EXCLUSION_DOMAINS.any? { |d| my_domain =~ /#{d}/ }

Upvotes: 2

James
James

Reputation: 4807

def excluded?(value)
  EXCLUSION_DOMAINS.any? { |domain| value.include? domain }
end

Upvotes: 2

Related Questions