Useless access modifier detectedRB-LI1077
An access modifier that has no code, or is repeated, is redundant.
Additionally, a public
access modifer, if used as the first access
modifier in a class, is redundant, since all methods are public by
default.
Some options are respected, which are the same as in Rubocop:
ContextCreatingMethods
: The default setting value is an empty array
that means no method is specified. This setting is an array of methods
which, when called, are known to create its own context in the module's
current access context.
MethodCreatingMethods
: The default setting value is an empty array that
means no method is specified. This setting is an array of methods which,
when called, are known to create other methods in the module's current access
context.
Bad practice
class Foo
# The following modifier is redundant (methods defined on the class'
# singleton class are not affected by the public modifier)
public
def self.method3
end
end
module Foo
# The following modifier is redundant. (Constants/methods defined directly
# in the module are not affected by the private modifier)
private
module Bar
def self.baz; end
end
end
class Foo
protected
define_method(:method2) do
end
protected # this is redundant (repeated from previous modifier)
[1,2,3].each do |i|
define_method("foo#{i}") do
end
end
end
Recommended
class Foo
private # this is not redundant (a method is defined)
def method2
end
end
class Foo
protected # this is not redundant (a method is defined)
define_method(:method2) do
end
end
class Foo
# The following is not redundant (conditionally defined methods are
# considered as always defining a method)
private
if condition?
def method
end
end
end
module Foo
def self.baz; end
private_class_method :baz # This is not redundant
end
In Ruby 2.1 or newer you can also combine the method definition with the access modifier, like so:
module Foo
def self.pub
"Public method"
end
private_class_method self.priv; "Private method"; end
end
Configurations
- ContextCreatingMethods:
# Lint/UselessAccessModifier: # ContextCreatingMethods: # - concerning
require 'active_support/concern' class Foo concerning :Bar do def some_public_method end private def some_private_method end end # this is not redundant because `concerning` created its own context private def some_other_private_method end end
- MethodCreatingMethods:
# Lint/UselessAccessModifier: # MethodCreatingMethods: # - delegate
# good require 'active_support/core_ext/module/delegation' class Foo # this is not redundant because `delegate` creates methods private delegate :method_a, to: :method_b end
References
- Private module methods in Ruby - Stackoverflow