【发布时间】:2019-02-01 22:32:08
【问题描述】:
我不确定如何最好地做到这一点:
我有一个函数可以查找 IP 并返回它来自的国家/地区。
country = Geolix.lookup(remote_ip).country.registered_country.name
有时它会失败,在这种情况下,没有定义任何数组键。
在其他语言中,我可能会这样做:
try do
country = Geolix.lookup(remote_ip).country.registered_country.name
rescue
country = nil
end
我真的很难让语法正确。这也感觉不是很“干净”;有没有更好的办法?
【问题讨论】: