-1

我有以下代码:

country_code = infer_country # will grab a user's two character country code
region = 'us' # united states by default
region_map = {
      "au" => ["au"], # australia
      "al" => ["al", "ba", "bg", "hr", "md", "me", "mk", "ro", "si"], # bulgaria and the balkans
      "cn" => ["cn"], # china
      "ee" => ["ee", "lt", "lv"], # estonia and the baltics
      "fi" => ["fi"], # finland
      "at" => ["at", "ch", "de"], # germany, austria, switzerland
      "cy" => ["cy", "gr", "mt"], # greece, cyprus, malta
      "hk" => ["hk"], # hong_kong
      "id" => ["id"], # indonesia
      "it" => ["it"], # italy
      "jp" => ["jp"], # japan
      "kp" => ["kp", "kr"], # korea
      "ar" => ["ar", "bl", "bo", "br", "bz", "cl", "co", "cr", "cu", "do", "ec", "gf", "gp", "gt", "hn", 
               "ht", "mf", "mq", "mx", "ni", "pa", "pe", "pr", "py", "sv", "uy", "ve"], # latin america including brazil
      "my" => ["my"], # malaysia
      "af" => ["af", "eg", "iq", "ir", "sa", "ye", "sy", "il", "jo", "ps", "lb", "om", "kw", "qa", "bh"], # middle east
      "nl" => ["nl"], # netherlands
      "no" => ["no"], # norway
      "pl" => ["pl"], # poland
      "pt" => ["pt"], # portugal
      "ph" => ["ph"], # philippines
      "ru" => ["ru"], # russia
      "rs" => ["rs"], # serbia
      "sg" => ["sg"], # singapore
      "za" => ["za"], # south africa
      "bn" => ["bn", "bu", "kh", "la", "tl", "vn"], # south east asia
      "es" => ["es"], # spain
      "tw" => ["tw"], # taiwan
      "th" => ["th"], # thailand
      "tr" => ["tr"], # turkey
      "gb" => ["gb" ] # united kingdom
    }.invert
 # version 0.0
 region_map.each do |key, value|
   if key.include? country_code
     region = value
     break
   end
 end

 puts region

如果country_code"gb",那么"gb"应该打印出来。如果country_code是在东南亚,说是"vn",那么"bn"应该打印出来。

我怎样才能优雅地解决这个问题?如有必要,我可以重组我的哈希。

4

3 回答 3

2
def find_region(country_code)
  @region_map.each {|k,v| return k if v.include? country_code}
  nil
end
于 2013-08-29T17:36:24.717 回答
2
def find_region(country_code)
  pair = @region_map.find{|k, v| v.include?(country_code)}
  pair && pair.first
end

find_region('gb') # => "gb"
find_region('bz') # => "ar"
find_region('lv') # => "ee"
find_region('ls') # => nil
于 2013-08-29T14:25:29.880 回答
0
region_map = Hash.new("us").merge(
  "au" => "au",
  "al" => "al",
  "ba" => "al",
  "bg" => "al",
  "hr" => "al",
  "md" => "al",
  "me" => "al",
  "mk" => "al",
  "ro" => "al",
  "si" => "al",
  "al" => "al", 
  "cn" => "cn",
  ...
)

region_map["non existing code"] # => "us"
region_map[nil] # => "us"
region_map["au"] # => "au"
region_map["ba"] # => "al"
region_map["cn"] # => "cn"

或者

def region_map code
  case code
  when "au"
    "au"
  when "al", "ba", "bg", "hr", "md", "me", "mk", "ro", "si"
    "al"
  when "cn"
    "cn"
  when "ee", "lt", "lv"
    "ee"
  ...
  else
    "us"
  end
end

region_map("non existing code") # => "us"
region_map(nil) # => "us"
region_map("au") # => "au"
region_map("ba") # => "al"
region_map("cn") # => "cn"
于 2013-08-29T14:37:42.180 回答