ruby-on-rails - rubocop 警察不同意:Layout/EmptyLineAfterGuardClause vs Layout/TrailingWhitespace
问题描述
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?
解决方案
要禁止cop 删除和 TrailingWhitespace
之间的行中的任何空格或制表符:return false if new_record?
bars.any?
def readonly?
return false if new_record?
bars.any?
end
尾随空格\s
是行尾的任何空格、制表符、回车,后面没有任何其他字符。
推荐阅读
- r - 如何创建一个函数来根据 R 中不同数据框中的值生成新变量
- sql - LEFT OUTER JOIN 的行为类似于 INNER JOIN
- powershell - 如何在powershell中复制zip文件夹
- c# - 在c#中删除文件中的项目
- ruby - 转储:厨师中的节点对象
- sql-server - SQL Server:动态透视临时表(动态 SQL)
- here-api - Here map API查询中如何避开高速公路
- database - 如何将 activemq DB 持久性配置到 Oracle?
- python - 如何使用 Selenium + Python 向下滚动对话框
- typescript - 如何理解打字稿中的“属性'名称'在'用户'类型中是私有的”