【问题标题】:disable Rubocop complaint about method禁用有关方法的 Rubocop 投诉
【发布时间】:2017-06-04 08:59:03
【问题描述】:

我有一个像 return if amount == 0 这样的方法,而 rubocop 抱怨它应该像 return if amount.zero?。我怎样才能让它跳过那个方法?这是我的 .rubocop.yml:

rubocop

StringLiterals:
  EnforcedStyle: double_quotes
  Enabled: true

Style/FrozenStringLiteralComment:
  Enabled: false

Style/TrailingCommaInLiteral:
  Enabled: false

Style/TrailingCommaInArguments:
  Enabled: false

Style/CaseEquality:
  Enabled: false

Documentation:
  Description: "Document classes and non-namespace modules."
  Enabled: false

Metrics/MethodLength:
  CountComments: false
  Max: 15

Rails/Delegate:
  Enabled: false

AllCops:
  Exclude:
    - db/**/*
    - config/**/*
    - script/**/*
    - vendor/**/*
    - bin/**/*
    - Rakefile
    - spec/spec_helper.rb
    - spec/rails_helper.rb
    - spec/teaspoon_env.rb

  TargetRubyVersion: 2.3

Rails:
  Enabled: true

submit_ticket_payment.rb

def call
  return if amount == 0
  Stripe::Charge.create(
    amount: amount,
    currency: "usd",
    description: event_name,
    receipt_email: context.rsvp.email,
    source: context.token,
    statement_descriptor: event_name
  )
rescue Stripe::StripeError
  context.fail!
end

那么基本上我怎么能让它不关心那个特定的实例呢?

【问题讨论】:

  • 愚蠢的问题,但为什么不听从它的建议呢?
  • zero? 方法会在预期为真时返回,我更喜欢== 0 的明确性质。

标签: ruby-on-rails rubocop


【解决方案1】:

如果您只想跳过此特定方法的规则,那么:

# rubocop:disable Style/NumericPredicate
def call
  return if amount == 0
  # ...
end
# rubocop:enable Style/NumericPredicate

【讨论】:

    【解决方案2】:

    有问题的检查由Style/NumericPredicate 警察执行。

    如果您通常希望启用它但不抱怨个别事件,您可以使用特殊注释暂时禁用它:

    # rubocop:disable Style/NumericPredicate
    return if amount == 0
    # rubocop:enable Style/NumericPredicate
    

    注意需要重新启用;否则它将跳过对文件整个其余部分的检查。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-16
      • 1970-01-01
      • 1970-01-01
      • 2020-12-04
      相关资源
      最近更新 更多