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
Rubocop does not throw a linting error
W: Lint/UselessAccessModifier: Useless private access modifier.
private
^^^^^^^
Define a private class method using the class singleton method and run rubocop
Include the output of rubocop -V. Here's an example:
$ rubocop -v
0.57.2
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!)
Most helpful comment
It's actually useless, as
other_class_methodis not private at all.You are confused by the fact that you are still calling
def self.other_class_methodYou have to remove the
selfpart. Currently you are not defining other_class_method on MyClass, but on it's singleton class, so the way to call it isMyClass.singleton_class.other_class_method(and it's not private!)