在代码块上使用保护子句
Using guard clause on code block
我有这个方法:
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']
and I am getting rubocop errors to use a guard clause instead of an
if-else statement... what would be the best way to do this?
首先仔细阅读几篇关于什么是保护条款的文章。
这是重构为使用保护子句的方法:
def self.get_image(product_id)
initialize
product_id = product_id.to_s
return cache_image(product_id) unless @image_db.key?(product_id)
return @image_db[product_id][':uri'] unless Time.now.to_i - @image_db[product_id][':cached_at'] > @refresh_period
puts Time.now.to_i - @image_db[product_id][':cached_at']
cache_image(product_id)
end
我可能会移出一些方法来简化它:
def self.get_image(product_id)
initialize
product_id = product_id.to_s
return cache_image(product_id) unless @image_db.key?(product_id)
return @image_db[product_id][':uri'] unless cached_at_gttn_refresh_period?(product_id)
puts Time.now.to_i - @image_db[product_id][':cached_at']
cache_image(product_id)
end
private
def cached_at_gttn_refresh_period?(product_id)
Time.now.to_i - @image_db[product_id][':cached_at'] > @refresh_period
end
我有这个方法:
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']
and I am getting rubocop errors to use a guard clause instead of an if-else statement... what would be the best way to do this?
首先仔细阅读几篇关于什么是保护条款的文章。
这是重构为使用保护子句的方法:
def self.get_image(product_id)
initialize
product_id = product_id.to_s
return cache_image(product_id) unless @image_db.key?(product_id)
return @image_db[product_id][':uri'] unless Time.now.to_i - @image_db[product_id][':cached_at'] > @refresh_period
puts Time.now.to_i - @image_db[product_id][':cached_at']
cache_image(product_id)
end
我可能会移出一些方法来简化它:
def self.get_image(product_id)
initialize
product_id = product_id.to_s
return cache_image(product_id) unless @image_db.key?(product_id)
return @image_db[product_id][':uri'] unless cached_at_gttn_refresh_period?(product_id)
puts Time.now.to_i - @image_db[product_id][':cached_at']
cache_image(product_id)
end
private
def cached_at_gttn_refresh_period?(product_id)
Time.now.to_i - @image_db[product_id][':cached_at'] > @refresh_period
end