Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?. How can I have it skip over that method? Here is my .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

So basically how can I have it not care about that particular instance?

like image 922
Bitwise Avatar asked Jan 19 '17 15:01

Bitwise


2 Answers

The check in question is implemented by the Style/NumericPredicate cop.

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.

like image 77
milgner Avatar answered Sep 20 '22 05:09

milgner


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
like image 33
Slava.K Avatar answered Sep 22 '22 05:09

Slava.K