ruby-on-rails - rubocop 警察不同意 : Layout/EmptyLineAfterGuardClause vs Layout/TrailingWhitespace

标签 ruby-on-rails ruby rubocop

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/

相关文章:

ruby-on-rails - 以数组作为属性的 Ruby 模型

ruby - Rspec Rake 任务 : How to parse a parameter?

javascript - jQuery 仅在本地 Rails 项目上的浏览器刷新后才起作用

ruby-on-rails - ActiveStorage 上传大型 base64 编码字符串?

ruby-on-rails - 如何为单个请求或代码块绕过 Rails.cache?

ruby-on-rails - 坚持如何在数组中使用 .first 和 .last

ruby-on-rails - 替换 URI.escape 以避免 Lint/UriEscapeUnescape 警告?

ruby - 样式/条件分配 : Use the return of the conditional for variable assignment and comparison

ruby-on-rails - 禁用卡住的字符串文字注释检查

sql - 在数据库中设置 bool 值