Coop Daddy
Coop Daddy

Reputation: 13

Check if String contains value in list

I have a list of addresses and want to flag if they are American or Canadian. I have a list of US states and CA Providences. Is there a more ruby-like way to do this:

us = false
address = '1234 Fake Address Ave N, Funtown, TX, 59595'
address = address.split(' ')
address.each do |part|
  if USStates.include? part
    us = true
  end
end

Upvotes: 0

Views: 74

Answers (2)

Coop Daddy
Coop Daddy

Reputation: 13

us = false
address.each { |part| us = true if USStates.include? parts }

Upvotes: 1

BTL
BTL

Reputation: 4656

Here is a one liner

address = '1234 Fake Address Ave N, Funtown, TX, 59595'
us = USStates.any? { |state| address.include?(state) }

Upvotes: 3

Related Questions