我有以下代码:
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"
应该打印出来。
我怎样才能优雅地解决这个问题?如有必要,我可以重组我的哈希。