Bitwise
Bitwise

Reputation: 8461

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?

Upvotes: 9

Views: 9700

Answers (3)

jmschp
jmschp

Reputation: 302

Well, you can either ignore the rule or try to refactor the code to be complaint, which is usual a better practice.

For the first option, ignoring the rule, apart from the cases already mentioned there is also:

Inline ignore with special comment:

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

And ignore in config file which is usual a better option, this way you have all your styles configuration in one place. You can use either the .rubocop.yml or the .rubocop_todo.yml, the purpose of the latter is to list all offenses, but work on refactoring to remove them one by one. The todo file can be generated with the CLI command rubocop --auto-gen-config.

Style/NumericPredicate:
  Exclude:
    - 'path/to/file.rb'

Now about refactoring, I understand the question is not about that, but I feel it is always good to mention. We all know what happens when we take the easy path of ignoring rules! :)

In this case let's suppose you can get in the amount variable either 0 or nil, for the condition to be met. The problem is that if you call nil.zero?, it will raise NoMethodError: undefined method zero?' for nil:NilClass`, what you can do here is use the Safe Navigation Operator:

&.

so your code could look something like this.

def call
  return if amount&.zero? || amount.nil?
  # ...
end

Upvotes: 0

Marcus Ilgner
Marcus Ilgner

Reputation: 7211

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.

Upvotes: 19

Slava.K
Slava.K

Reputation: 3080

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

Upvotes: 8

Related Questions