Ruby 2.6.5
Rails 5.2.3
当我跑rubocop app/models/foo.rb
时,我得到:
app/models/foo.rb:24:5: C: Layout/EmptyLineAfterGuardClause: Add empty line after guard clause.
return false if new_record?
^^^^^^^^^^^^^^^^^^^^^^^^^^^
1 file inspected, 1 offense detected
所以我做出了改变:
# before
def readonly?
return false if new_record?
bars.any?
end
#after
def readonly?
return false if new_record?
bars.any?
end
得到:
app/models/foo.rb:25:1: C: Layout/TrailingWhitespace: Trailing whitespace detected.
1 file inspected, 1 offense detected
解决一个问题会引发另一个问题,反之亦然。
如果我希望这个文件通过 rubocop 并在 Ruby/Rails 方面表现良好,那么最好忽略哪个 cop?
最佳答案
压制TrailingWhitespace
cop 删除 return false if new_record?
之间的行中的任何空格或制表符和bars.any?
:
def readonly?
return false if new_record?
bars.any?
end
尾随空格 \s
是行尾的任何空格、制表符、回车符,后面没有任何其他字符。
关于ruby-on-rails - rubocop 警察不同意 : Layout/EmptyLineAfterGuardClause vs Layout/TrailingWhitespace,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59004940/