Naming
Naming/AccessorMethodName
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
No |
0.50 |
- |
Makes sure that accessor methods are named properly. Applies to both instance and class methods.
Offenses are only registered for methods with the expected
arity. Getters (get_attribute ) must have no arguments to be
registered, and setters (set_attribute(value) ) must have exactly
one.
|
Naming/AsciiIdentifiers
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
No |
0.50 |
0.87 |
Checks for non-ascii characters in identifier and constant names. Identifiers are always checked and whether constants are checked can be controlled using AsciiConstants config.
Examples
# bad
def καλημερα # Greek alphabet (non-ascii)
end
# bad
def こんにちはと言う # Japanese character (non-ascii)
end
# bad
def hello_🍣 # Emoji (non-ascii)
end
# good
def say_hello
end
# bad
신장 = 10 # Hangul character (non-ascii)
# good
height = 10
# bad
params[:عرض_gteq] # Arabic character (non-ascii)
# good
params[:width_gteq]
Naming/BinaryOperatorParameterName
Naming/BlockForwarding
Requires Ruby version 3.1 |
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Pending |
Yes |
Always |
1.24 |
- |
In Ruby 3.1, anonymous block forwarding has been added.
This cop identifies places where do_something(&block)
can be replaced
by do_something(&)
.
It also supports the opposite style by alternative explicit
option.
You can specify the block variable name for autocorrection with BlockForwardingName
.
The default variable name is block
. If the name is already in use, it will not be
autocorrected.
Examples
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle |
|
|
BlockForwardingName |
|
String |
Naming/BlockParameterName
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
No |
0.53 |
0.77 |
Checks block parameter names for how descriptive they are. It is highly configurable.
The MinNameLength
config option takes an integer. It represents
the minimum amount of characters the name must be. Its default is 1.
The AllowNamesEndingInNumbers
config option takes a boolean. When
set to false, this cop will register offenses for names ending with
numbers. Its default is false. The AllowedNames
config option
takes an array of permitted names that will never register an
offense. The ForbiddenNames
config option takes an array of
restricted names that will always register an offense.
Examples
# bad
bar do |varOne, varTwo|
varOne + varTwo
end
# With `AllowNamesEndingInNumbers` set to false
foo { |num1, num2| num1 * num2 }
# With `MinNameLength` set to number greater than 1
baz { |a, b, c| do_stuff(a, b, c) }
# good
bar do |thud, fred|
thud + fred
end
foo { |speed, distance| speed * distance }
baz { |age, height, gender| do_stuff(age, height, gender) }
Naming/ClassAndModuleCamelCase
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
No |
0.50 |
0.85 |
Checks for class and module names with an underscore in them.
AllowedNames
config takes an array of permitted names.
Its default value is ['module_parent']
.
These names can be full class/module names or part of the name.
eg. Adding my_class
to the AllowedNames
config will allow names like
my_class
, my_class::User
, App::my_class
, App::my_class::User
, etc.
Examples
# bad
class My_Class
end
module My_Module
end
# good
class MyClass
end
module MyModule
end
class module_parent::MyModule
end
Naming/ConstantName
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
No |
0.50 |
- |
Checks whether constant names are written using SCREAMING_SNAKE_CASE.
To avoid false positives, it ignores cases in which we cannot know for certain the type of value that would be assigned to a constant.
Naming/FileName
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
No |
0.50 |
1.23 |
Makes sure that Ruby source files have snake_case names. Ruby scripts (i.e. source files with a shebang in the first line) are ignored.
The cop also ignores .gemspec
files, because Bundler
recommends using dashes to separate namespaces in nested gems
(i.e. bundler-console
becomes Bundler::Console
). As such, the
gemspec is supposed to be named bundler-console.gemspec
.
When ExpectMatchingDefinition
(default: false
) is true
, the cop requires
each file to have a class, module or Struct
defined in it that matches
the filename. This can be further configured using
CheckDefinitionPathHierarchy
(default: true
) to determine whether the
path should match the namespace of the above definition.
When IgnoreExecutableScripts
(default: true
) is true
, files that start
with a shebang line are not considered by the cop.
When Regex
is set, the cop will flag any filename that does not match
the regular expression.
Examples
# bad
lib/layoutManager.rb
anything/usingCamelCase
# good
lib/layout_manager.rb
anything/using_snake_case.rake
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
Exclude |
|
Array |
ExpectMatchingDefinition |
|
Boolean |
CheckDefinitionPathHierarchy |
|
Boolean |
CheckDefinitionPathHierarchyRoots |
|
Array |
Regex |
|
|
IgnoreExecutableScripts |
|
Boolean |
AllowedAcronyms |
|
Array |
Naming/HeredocDelimiterCase
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Always |
0.50 |
1.2 |
Checks that your heredocs are using the configured case. By default it is configured to enforce uppercase heredocs.
Examples
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle |
|
|
Naming/HeredocDelimiterNaming
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
No |
0.50 |
- |
Checks that your heredocs are using meaningful delimiters.
By default it disallows END
and EO*
, and can be configured through
forbidden listing additional delimiters.
Examples
# good
<<-SQL
SELECT * FROM foo
SQL
# bad
<<-END
SELECT * FROM foo
END
# bad
<<-EOS
SELECT * FROM foo
EOS
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
ForbiddenDelimiters |
|
Array |
Naming/InclusiveLanguage
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Disabled |
Yes |
Always |
1.18 |
1.49 |
Recommends the use of inclusive language instead of problematic terms. The cop can check the following locations for offenses:
-
identifiers
-
constants
-
variables
-
strings
-
symbols
-
comments
-
file paths
Each of these locations can be individually enabled/disabled via configuration, for example CheckIdentifiers = true/false.
Flagged terms are configurable for the cop. For each flagged term an optional
Regex can be specified to identify offenses. Suggestions for replacing a flagged term can
be configured and will be displayed as part of the offense message.
An AllowedRegex can be specified for a flagged term to exempt allowed uses of the term.
WholeWord: true
can be set on a flagged term to indicate the cop should only match when
a term matches the whole word (partial matches will not be offenses).
The cop supports autocorrection when there is only one suggestion. When there are multiple suggestions, the best suggestion cannot be identified and will not be autocorrected.
Examples
FlaggedTerms: { whitelist: { Suggestions: ['allowlist'] } }
# Suggest replacing identifier whitelist with allowlist
# bad
whitelist_users = %w(user1 user1)
# good
allowlist_users = %w(user1 user2)
FlaggedTerms: { master: { Suggestions: ['main', 'primary', 'leader'] } }
# Suggest replacing master in an instance variable name with main, primary, or leader
# bad
@master_node = 'node1.example.com'
# good
@primary_node = 'node1.example.com'
FlaggedTerms: { whitelist: { Regex: !ruby/regexp '/white[-_\s]?list' } }
# Identify problematic terms using a Regexp
# bad
white_list = %w(user1 user2)
# good
allow_list = %w(user1 user2)
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
CheckIdentifiers |
|
Boolean |
CheckConstants |
|
Boolean |
CheckVariables |
|
Boolean |
CheckStrings |
|
Boolean |
CheckSymbols |
|
Boolean |
CheckComments |
|
Boolean |
CheckFilepaths |
|
Boolean |
FlaggedTerms |
|
Naming/MemoizedInstanceVariableName
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
No |
Always (Unsafe) |
0.53 |
1.2 |
Checks for memoized methods whose instance variable name
does not match the method name. Applies to both regular methods
(defined with def
) and dynamic methods (defined with
define_method
or define_singleton_method
).
This cop can be configured with the EnforcedStyleForLeadingUnderscores directive. It can be configured to allow for memoized instance variables prefixed with an underscore. Prefixing ivars with an underscore is a convention that is used to implicitly indicate that an ivar should not be set or referenced outside of the memoization method.
Safety
This cop relies on the pattern @instance_var ||= …
,
but this is sometimes used for other purposes than memoization
so this cop is considered unsafe. Also, its autocorrection is unsafe
because it may conflict with instance variable names already in use.
Examples
EnforcedStyleForLeadingUnderscores: disallowed (default)
# bad
# Method foo is memoized using an instance variable that is
# not `@foo`. This can cause confusion and bugs.
def foo
@something ||= calculate_expensive_thing
end
def foo
return @something if defined?(@something)
@something = calculate_expensive_thing
end
# good
def _foo
@foo ||= calculate_expensive_thing
end
# good
def foo
@foo ||= calculate_expensive_thing
end
# good
def foo
@foo ||= begin
calculate_expensive_thing
end
end
# good
def foo
helper_variable = something_we_need_to_calculate_foo
@foo ||= calculate_expensive_thing(helper_variable)
end
# good
define_method(:foo) do
@foo ||= calculate_expensive_thing
end
# good
define_method(:foo) do
return @foo if defined?(@foo)
@foo = calculate_expensive_thing
end
EnforcedStyleForLeadingUnderscores: required
# bad
def foo
@something ||= calculate_expensive_thing
end
# bad
def foo
@foo ||= calculate_expensive_thing
end
def foo
return @foo if defined?(@foo)
@foo = calculate_expensive_thing
end
# good
def foo
@_foo ||= calculate_expensive_thing
end
# good
def _foo
@_foo ||= calculate_expensive_thing
end
def foo
return @_foo if defined?(@_foo)
@_foo = calculate_expensive_thing
end
# good
define_method(:foo) do
@_foo ||= calculate_expensive_thing
end
# good
define_method(:foo) do
return @_foo if defined?(@_foo)
@_foo = calculate_expensive_thing
end
EnforcedStyleForLeadingUnderscores :optional
# bad
def foo
@something ||= calculate_expensive_thing
end
# good
def foo
@foo ||= calculate_expensive_thing
end
# good
def foo
@_foo ||= calculate_expensive_thing
end
# good
def _foo
@_foo ||= calculate_expensive_thing
end
# good
def foo
return @_foo if defined?(@_foo)
@_foo = calculate_expensive_thing
end
# good
define_method(:foo) do
@foo ||= calculate_expensive_thing
end
# good
define_method(:foo) do
@_foo ||= calculate_expensive_thing
end
Naming/MethodName
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
No |
0.50 |
- |
Makes sure that all methods use the configured style, snake_case or camelCase, for their names.
This cop has AllowedPatterns
configuration option.
Naming/MethodName: AllowedPatterns: - '\AonSelectionBulkChange\z' - '\AonSelectionCleared\z'
Method names matching patterns are always allowed.
Naming/MethodParameterName
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
No |
0.53 |
0.77 |
Checks method parameter names for how descriptive they are. It is highly configurable.
The MinNameLength
config option takes an integer. It represents
the minimum amount of characters the name must be. Its default is 3.
The AllowNamesEndingInNumbers
config option takes a boolean. When
set to false, this cop will register offenses for names ending with
numbers. Its default is false. The AllowedNames
config option
takes an array of permitted names that will never register an
offense. The ForbiddenNames
config option takes an array of
restricted names that will always register an offense.
Examples
# bad
def bar(varOne, varTwo)
varOne + varTwo
end
# With `AllowNamesEndingInNumbers` set to false
def foo(num1, num2)
num1 * num2
end
# With `MinNameLength` set to number greater than 1
def baz(a, b, c)
do_stuff(a, b, c)
end
# good
def bar(thud, fred)
thud + fred
end
def foo(speed, distance)
speed * distance
end
def baz(age_a, height_b, gender_c)
do_stuff(age_a, height_b, gender_c)
end
Naming/PredicateName
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
No |
0.50 |
0.77 |
Checks that predicate method names end with a question mark and do not start with a forbidden prefix.
A method is determined to be a predicate method if its name starts with
one of the prefixes listed in the NamePrefix
configuration. The list
defaults to is_
, has_
, and have_
but may be overridden.
Predicate methods must end with a question mark.
When ForbiddenPrefixes
is also set (as it is by default), predicate
methods which begin with a forbidden prefix are not allowed, even if
they end with a ?
. These methods should be changed to remove the
prefix.
Examples
NamePrefix: ['is_', 'has_', 'have_'] (default)
# bad
def is_even(value)
end
# When ForbiddenPrefixes: ['is_', 'has_', 'have_'] (default)
# good
def even?(value)
end
# When ForbiddenPrefixes: []
# good
def is_even?(value)
end
NamePrefix: ['seems_to_be_']
# bad
def seems_to_be_even(value)
end
# When ForbiddenPrefixes: ['seems_to_be_']
# good
def even?(value)
end
# When ForbiddenPrefixes: []
# good
def seems_to_be_even?(value)
end
AllowedMethods: ['is_a?'] (default)
# Despite starting with the `is_` prefix, this method is allowed
# good
def is_a?(value)
end
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
NamePrefix |
|
Array |
ForbiddenPrefixes |
|
Array |
AllowedMethods |
|
Array |
MethodDefinitionMacros |
|
Array |
Exclude |
|
Array |
Naming/RescuedExceptionsVariableName
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Always |
0.67 |
0.68 |
Makes sure that rescued exceptions variables are named as expected.
The PreferredName
config option takes a String
. It represents
the required name of the variable. Its default is e
.
This cop does not consider nested rescues because it cannot guarantee that the variable from the outer rescue is not used within the inner rescue (in which case, changing the inner variable would shadow the outer variable). |
Naming/VariableName
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
No |
0.50 |
1.8 |
Makes sure that all variables use the configured style, snake_case or camelCase, for their names.
Naming/VariableNumber
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
No |
0.50 |
1.4 |
Makes sure that all numbered variables use the configured style, snake_case, normalcase, or non_integer, for their numbering.
Additionally, CheckMethodNames
and CheckSymbols
configuration options
can be used to specify whether method names and symbols should be checked.
Both are enabled by default.
Examples
EnforcedStyle: normalcase (default)
# bad
:some_sym_1
variable_1 = 1
def some_method_1; end
def some_method1(arg_1); end
# good
:some_sym1
variable1 = 1
def some_method1; end
def some_method1(arg1); end
EnforcedStyle: snake_case
# bad
:some_sym1
variable1 = 1
def some_method1; end
def some_method_1(arg1); end
# good
:some_sym_1
variable_1 = 1
def some_method_1; end
def some_method_1(arg_1); end
EnforcedStyle: non_integer
# bad
:some_sym1
:some_sym_1
variable1 = 1
variable_1 = 1
def some_method1; end
def some_method_1; end
def some_methodone(arg1); end
def some_methodone(arg_1); end
# good
:some_symone
:some_sym_one
variableone = 1
variable_one = 1
def some_methodone; end
def some_method_one; end
def some_methodone(argone); end
def some_methodone(arg_one); end
# In the following examples, we assume `EnforcedStyle: normalcase` (default).