简体   繁体   English

禁用Rubocop关于方法的投诉

[英]disable Rubocop complaint about method

I have a method that goes like this return if amount == 0 and rubocop is complaining that it should be like return if amount.zero? return if amount == 0 ,我有一个像这样return if amount == 0的方法,并且rubocop抱怨它应该像return if amount.zero?那样return if amount.zero? . How can I have it skip over that method? 我怎么能跳过这个方法呢? Here is my .rubocop.yml: 这是我的.rubocop.yml:

rubocop 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 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

So basically how can I have it not care about that particular instance? 所以基本上我怎么能不关心那个特定的实例呢?

The check in question is implemented by the Style/NumericPredicate cop. 有问题的检查由Style/NumericPredicate警察实施。

If you generally want it to be enabled but not complain about an individual occurence, you can temporarily disable it with a special comment: 如果您通常希望启用它但不抱怨个别出现,您可以使用特殊注释暂时禁用它:

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

Note that it needs to be enabled again; 请注意,它需要再次启用; otherwise it will skip the check for the whole remainder of the file. 否则它将跳过检查文件的整个剩余部分。

If you wish to skip rule only for this particular method then: 如果您希望仅针对此特定方法跳过规则,则:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM