我有这个方法:
def self.get_image(product_id)
self.initialize
product_id=product_id.to_s
if @image_db.key?(product_id)
if Time.now.to_i - @image_db[product_id][':cached_at'] > @refresh_period
puts Time.now.to_i - @image_db[product_id][':cached_at']
self.cache_image(product_id)
else
return @image_db[product_id][':uri']
end
else
self.cache_image(product_id)
end
end
并且我收到 rubocop 错误以使用保护子句而不是if
-else
语句。最好的方法是什么?
我正在考虑这段代码:
def self.get_image(product_id)
self.initialize
product_id=product_id.to_s
return if @image_db.key?(product_id)
return if Time.now.to_i - @image_db[product_id][':cached_at'] > @refresh_period
puts Time.now.to_i - @image_db[product_id][':cached_at']
self.cache_image(product_id)
end
但这条线永远不会被调用:
return @image_db[product_id][':uri']