Rubocop: Lint/UselessAccessModifier - being thrown on class singleton self definition

Created on 5 Jul 2018  路  1Comment  路  Source: rubocop-hq/rubocop

When I use the class singleton format, as below, Rubocop tells me it's a useless private access modifier:

class MyClass
  def self.class_method
    # stuff
  end

  class << self
    private

    def self.other_class_method
        # stuff
    end
  end
end

When I use the private_class_method definition format, as below, Rubocop does not tell me it's useless:

class MyClass
  def self.class_method
    # stuff
  end

  def self.other_class_method
    # stuff
  end

  private_class_method :other_class_method
end

Expected behavior

Rubocop does not throw a linting error

Actual behavior

W: Lint/UselessAccessModifier: Useless private access modifier.
    private
    ^^^^^^^

Steps to reproduce the problem

Define a private class method using the class singleton method and run rubocop

RuboCop version

Include the output of rubocop -V. Here's an example:

$ rubocop -v
0.57.2

Most helpful comment

It's actually useless, as other_class_method is not private at all.
You are confused by the fact that you are still calling def self.other_class_method
You have to remove the self part. Currently you are not defining other_class_method on MyClass, but on it's singleton class, so the way to call it is MyClass.singleton_class.other_class_method (and it's not private!)

>All comments

It's actually useless, as other_class_method is not private at all.
You are confused by the fact that you are still calling def self.other_class_method
You have to remove the self part. Currently you are not defining other_class_method on MyClass, but on it's singleton class, so the way to call it is MyClass.singleton_class.other_class_method (and it's not private!)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

printercu picture printercu  路  3Comments

david942j picture david942j  路  3Comments

bquorning picture bquorning  路  3Comments

millisami picture millisami  路  3Comments

benoittgt picture benoittgt  路  3Comments