在RuboCop配置文件中,可以使用对应的参数来指定要使用的Ruby版本。但是,在AllCops传递TargetRubyVersion参数时,很多用户会发现它似乎没有任何影响。这是因为该参数只对检查非标准库代码的文件时才起作用。如果要检查标准库代码,请使用RubyVersion参数。以下是一个示例配置文件,其中已经使用了TargetRubyVersion参数:
# .rubocop.yml
require:
- rubocop-rails
AllCops:
TargetRubyVersion: 2.7
Exclude:
- 'db/schema.rb'
- 'vendor/**/*'
- 'config/locales/**/*'
- 'bin/**/*'
Rails:
Enabled: true
Style/FrozenStringLiteralComment:
EnforcedStyle: always
如果您想要检查标准库代码,请使用RubyVersion参数,如下示例所示:
# .rubocop.yml
require:
- rubocop-rails
AllCops:
RubyVersion: 2.7
Exclude:
- 'db/schema.rb'
- 'vendor/**/*'
- 'config/locales/**/*'
- 'bin/**/*'
Rails:
Enabled: true
Style/FrozenStringLiteralComment:
EnforcedStyle: always