【发布时间】:2018-01-29 21:03:38
【问题描述】:
为了获得我这样写的网站图标二进制文件。
def favicon_url_from_link
# get a url from link tag
URI('http://example.com/common/favicon.png')
end
def favicon_url_from_path
URI('http://example.com/favicon.ico')
end
def favicon_binary_from_link
favicon_url_from_link&.read
rescue OpenURI::HTTPError
nil
end
def favicon_binary_from_path
favicon_url_from_path&.read
rescue OpenURI::HTTPError
nil
end
def favicon_binary
favicon_binary_from_link || favicon_binary_from_path
end
但我认为为每个 url 编写 rescue 子句有点多余。
怎么写更简洁?
【问题讨论】: