繁体   English   中英

在Ruby中如何在一个保护子句中放置多行?

[英]In Ruby how to put multiple lines in one guard clause?

我有以下代码行:

if params[:"available_#{district.id}"] == 'true'
    @deliverycharge = @product.deliverycharges.create!(districtrate_id: district.id)
    delivery_custom_price(district)
end

Rubocop突出显示它,并要求我使用保护条款。 我该怎么做?

编辑:Rubocop突出显示第一行并给出此消息Use a guard clause instead of wrapping the code inside a conditional expression

不知道周围的代码是什么样的,所以让我们假设你的代码是方法的整个主体。 然后一个保护条款可能如下所示:

def some_method
  return if params[:"available_#{district.id}"] != 'true'   #guard clause

  @deliverycharge = @product.deliverycharges.create!(districtrate_id: district.id)
  delivery_custom_price(district)
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM