RuboCop:线路太长 ← 如何忽略?

RuboCop: Line is too long ← How to Ignore?

我刚刚将 RuboCop 添加到 rails 项目并安装了 Sublime 包以在编辑器中查看 RuboCop 建议。我想弄清楚如何将最大行长度从 80 个字符更改为 80 个字符,或者完全忽略该规则。

目前正在使用:

在您的代码中,您可以像这样禁用一堆行:

# rubocop:disable Layout/LineLength
puts "This line is lonnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnng"
# rubocop:enable Layout/LineLength

或将此添加到您的 .rubocop.yml 文件以增加最大长度:

Layout/LineLength:
  Max: 100

在项目的根目录中创建一个 .rubocop.yml 文件(注意文件名中的初始 .),您将有很多选项(查看注释了解您的Rubocop 版本用作 the way to handle LineLength has changed):

Metrics/LineLength: # for Rubocop < 0.78.0
Layout/LineLength: # for Rubocop >= 0.78.0
  # This will disable the rule completely, regardless what other options you put
  Enabled: false
  # Change the default 80 chars limit value
  Max: 120
  # If you want the rule only apply to a specific folder/file
  Include:
    - 'app/**/*'
  # If you want the rule not to apply to a specific folder/file
  Exclude:
    - 'db/schema.rb'

根据 rubocop gem 版本 0.78.0 在 18-12-2019 的最新更改,从现在起 LineLength cop 从度量部门转移到布局部门。所以基本上如果有人需要使用高于 0.78.0 的版本号来禁用长行应该这样做。

# rubocop:disable Layout/LineLength
  "I'm a really long line"
# rubocop:enable Layout/LineLength

还有.rubocop.yml配置改成这样

Layout/LineLength:
  Max: 100

为了获取 rubocop 更改日志,click here

您可以查看此以禁用单行的 cop。

示例:

puts "This line is lonnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnng" # rubocop:disable Layout/LineLength