【发布时间】:2017-03-19 16:15:25
【问题描述】:
我有一段代码,其中有一个带有保护子句的 raise 语句:
def validate_index index
# Change to SizeError
raise ArgumentError, "Size of index (#{index.size}) does not matches"\
"size of vector (#{size})" if size != index.size
end
在这一点上,rubocop 给出了冒犯:
Style/MultilineIfModifier: Favor a normal if-statement over a modifier clause in a multiline statement.
我将我的代码修改为正常 if else case 如下:
def validate_index index
# Change to SizeError
if size != index.size
raise ArgumentError, "Size of index (#{index.size}) does not matches"\
"size of vector (#{size})"
end
end
但现在它犯了这个罪行:
Style/GuardClause: Use a guard clause instead of wrapping the code inside a conditional expression.
遇到这种情况怎么办?两者都在引发错误。还有其他选择吗?
【问题讨论】:
-
您不必盲目地遵循 rubocop 所说的一切。您可以在
.rubocop.yml中禁用 GuardClause 样式检查 -
您还可以使用 # rubocop:disable 和 # rubocop:enable 禁用内联的单个项目