Style
Style/AccessModifierDeclarations
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
No |
0.57 |
0.81 |
Access modifiers should be declared to apply to a group of methods or inline before each method, depending on configuration. EnforcedStyle config covers only method definitions. Applications of visibility methods to symbols can be controlled using AllowModifiersOnSymbols config.
Style/AccessorGrouping
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.87 |
- |
This cop checks for grouping of accessors in class
and module
bodies.
By default it enforces accessors to be placed in grouped declarations,
but it can be configured to enforce separating them in multiple declarations.
Sorbet is not compatible with "grouped" style. Consider "separated" style
or disabling this cop.
|
Style/Alias
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.9 |
0.36 |
This cop enforces the use of either #alias
or #alias_method
depending on configuration.
It also flags uses of alias :symbol
rather than alias bareword
.
Examples
Style/AndOr
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes (Unsafe) |
0.9 |
1.21 |
This cop checks for uses of and
and or
, and suggests using &&
and
||
instead. It can be configured to check only in conditions or in
all contexts.
Safety
Auto-correction is unsafe because there is a different operator precedence
between logical operators (&&
and ||
) and semantic operators (and
and or
),
and that might change the behaviour.
Examples
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle |
|
|
Style/ArgumentsForwarding
Required Ruby version: 2.7 |
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Pending |
Yes |
Yes |
1.1 |
- |
In Ruby 2.7, arguments forwarding has been added.
This cop identifies places where do_something(*args, &block)
can be replaced by do_something(…)
.
Examples
# bad
def foo(*args, &block)
bar(*args, &block)
end
# bad
def foo(*args, **kwargs, &block)
bar(*args, **kwargs, &block)
end
# good
def foo(...)
bar(...)
end
Style/ArrayCoercion
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Disabled |
No |
Yes (Unsafe) |
0.88 |
- |
This cop enforces the use of Array()
instead of explicit Array
check or [*var]
.
The cop is disabled by default due to safety concerns.
Safety
This cop is unsafe because a false positive may occur if
the argument of Array()
is (or could be) nil or depending
on how the argument is handled by Array()
(which can be
different than just wrapping the argument in an array).
For example:
[nil] #=> [nil]
Array(nil) #=> []
[{a: 'b'}] #= [{a: 'b'}]
Array({a: 'b'}) #=> [[:a, 'b']]
[Time.now] #=> [#<Time ...>]
Array(Time.now) #=> [14, 16, 14, 16, 9, 2021, 4, 259, true, "EDT"]
Examples
# bad
paths = [paths] unless paths.is_a?(Array)
paths.each { |path| do_something(path) }
# bad (always creates a new Array instance)
[*paths].each { |path| do_something(path) }
# good (and a bit more readable)
Array(paths).each { |path| do_something(path) }
Style/ArrayJoin
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.20 |
0.31 |
This cop checks for uses of "*" as a substitute for join.
Not all cases can reliably checked, due to Ruby’s dynamic types, so we consider only cases when the first argument is an array literal or the second is a string literal.
Style/AsciiComments
Style/Attr
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.9 |
0.12 |
This cop checks for uses of Module#attr.
Examples
# bad - creates a single attribute accessor (deprecated in Ruby 1.9)
attr :something, true
attr :one, :two, :three # behaves as attr_reader
# good
attr_accessor :something
attr_reader :one, :two, :three
Style/AutoResourceCleanup
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Disabled |
Yes |
No |
0.30 |
- |
This cop checks for cases when you could use a block accepting version of a method that does automatic resource cleanup.
Style/BarePercentLiterals
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.25 |
- |
This cop checks if usage of %() or %Q() matches configuration.
Examples
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle |
|
|
Style/BeginBlock
Style/BisectedAttrAccessor
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.87 |
- |
This cop checks for places where attr_reader
and attr_writer
for the same method can be combined into single attr_accessor
.
Style/BlockComments
Style/BlockDelimiters
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.30 |
0.35 |
Check for uses of braces or do/end around single line or multi-line blocks.
Methods that can be either procedural or functional and cannot be
categorised from their usage alone is ignored.
lambda
, proc
, and it
are their defaults.
Additional methods can be added to the IgnoredMethods
.
Examples
EnforcedStyle: line_count_based (default)
# bad - single line block
items.each do |item| item / 5 end
# good - single line block
items.each { |item| item / 5 }
# bad - multi-line block
things.map { |thing|
something = thing.some_method
process(something)
}
# good - multi-line block
things.map do |thing|
something = thing.some_method
process(something)
end
EnforcedStyle: semantic
# Prefer `do...end` over `{...}` for procedural blocks.
# return value is used/assigned
# bad
foo = map do |x|
x
end
puts (map do |x|
x
end)
# return value is not used out of scope
# good
map do |x|
x
end
# Prefer `{...}` over `do...end` for functional blocks.
# return value is not used out of scope
# bad
each { |x|
x
}
# return value is used/assigned
# good
foo = map { |x|
x
}
map { |x|
x
}.inspect
# The AllowBracesOnProceduralOneLiners option is ignored unless the
# EnforcedStyle is set to `semantic`. If so:
# If the AllowBracesOnProceduralOneLiners option is unspecified, or
# set to `false` or any other falsey value, then semantic purity is
# maintained, so one-line procedural blocks must use do-end, not
# braces.
# bad
collection.each { |element| puts element }
# good
collection.each do |element| puts element end
# If the AllowBracesOnProceduralOneLiners option is set to `true`, or
# any other truthy value, then one-line procedural blocks may use
# either style. (There is no setting for requiring braces on them.)
# good
collection.each { |element| puts element }
# also good
collection.each do |element| puts element end
EnforcedStyle: braces_for_chaining
# bad
words.each do |word|
word.flip.flop
end.join("-")
# good
words.each { |word|
word.flip.flop
}.join("-")
EnforcedStyle: always_braces
# bad
words.each do |word|
word.flip.flop
end
# good
words.each { |word|
word.flip.flop
}
BracesRequiredMethods: ['sig']
# Methods listed in the BracesRequiredMethods list, such as 'sig'
# in this example, will require `{...}` braces. This option takes
# precedence over all other configurations except IgnoredMethods.
# bad
sig do
params(
foo: string,
).void
end
def bar(foo)
puts foo
end
# good
sig {
params(
foo: string,
).void
}
def bar(foo)
puts foo
end
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle |
|
|
ProceduralMethods |
|
Array |
FunctionalMethods |
|
Array |
IgnoredMethods |
|
Array |
AllowBracesOnProceduralOneLiners |
|
Boolean |
BracesRequiredMethods |
|
Array |
Style/CaseEquality
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.9 |
0.89 |
This cop checks for uses of the case equality operator(===).
If AllowOnConstant
option is enabled, the cop will ignore violations when the receiver of
the case equality operator is a constant.
Examples
# bad
(1..100) === 7
/something/ === some_string
# good
something.is_a?(Array)
(1..100).include?(7)
/something/.match?(some_string)
Style/CaseLikeIf
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
No |
Yes (Unsafe) |
0.88 |
- |
This cop identifies places where if-elsif
constructions
can be replaced with case-when
.
Safety
This cop is unsafe. case
statements use ===
for equality,
so if the original conditional used a different equality operator, the
behaviour may be different.
Examples
# bad
if status == :active
perform_action
elsif status == :inactive || status == :hibernating
check_timeout
else
final_action
end
# good
case status
when :active
perform_action
when :inactive, :hibernating
check_timeout
else
final_action
end
Style/CharacterLiteral
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.9 |
- |
Checks for uses of the character literal ?x. Starting with Ruby 1.9 character literals are essentially one-character strings, so this syntax is mostly redundant at this point.
? character literal can be used to express meta and control character. That’s a good use case of ? literal so it doesn’t count it as an offense.
Style/ClassAndModuleChildren
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes (Unsafe) |
0.19 |
- |
This cop checks the style of children definitions at classes and modules. Basically there are two different styles:
The compact style is only forced for classes/modules with one child.
Safety
Autocorrection is unsafe.
Moving from compact to nested children requires knowledge of whether the outer parent is a module or a class. Moving from nested to compact requires verification that the outer parent is defined elsewhere. Rubocop does not have the knowledge to perform either operation safely and thus requires manual oversight.
Examples
Style/ClassCheck
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.24 |
- |
This cop enforces consistent use of Object#is_a?
or Object#kind_of?
.
Examples
Style/ClassEqualityComparison
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.93 |
- |
This cop enforces the use of Object#instance_of?
instead of class comparison
for equality.
Examples
# bad
var.class == Date
var.class.equal?(Date)
var.class.eql?(Date)
var.class.name == 'Date'
# good
var.instance_of?(Date)
Style/ClassMethods
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.9 |
0.20 |
This cop checks for uses of the class/module name instead of self, when defining class/module methods.
Style/ClassMethodsDefinitions
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Disabled |
Yes |
Yes |
0.89 |
- |
This cop enforces using def self.method_name
or class << self
to define class methods.
Examples
EnforcedStyle: def_self (default)
# bad
class SomeClass
class << self
attr_accessor :class_accessor
def class_method
# ...
end
end
end
# good
class SomeClass
def self.class_method
# ...
end
class << self
attr_accessor :class_accessor
end
end
# good - contains private method
class SomeClass
class << self
attr_accessor :class_accessor
private
def private_class_method
# ...
end
end
end
Style/ClassVars
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
No |
0.13 |
- |
This cop checks for uses of class variables. Offenses are signaled only on assignment to class variables to reduce the number of offenses that would be reported.
You have to be careful when setting a value for a class variable; if a class has been inherited, changing the value of a class variable also affects the inheriting classes. This means that it’s almost always better to use a class instance variable instead.
Examples
# bad
class A
@@test = 10
end
class A
def self.test(name, value)
class_variable_set("@@#{name}", value)
end
end
class A; end
A.class_variable_set(:@@test, 10)
# good
class A
@test = 10
end
class A
def test
@@test # you can access class variable without offense
end
end
class A
def self.test(name)
class_variable_get("@@#{name}") # you can access without offense
end
end
Style/CollectionCompact
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Pending |
No |
Yes (Unsafe) |
1.2 |
1.3 |
This cop checks for places where custom logic on rejection nils from arrays
and hashes can be replaced with {Array,Hash}#{compact,compact!}
.
Safety
It is unsafe by default because false positives may occur in the
nil
check of block arguments to the receiver object.
For example, [[1, 2], [3, nil]].reject { |first, second| second.nil? }
and [[1, 2], [3, nil]].compact
are not compatible. This will work fine
when the receiver is a hash object.
Style/CollectionMethods
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Disabled |
No |
Yes (Unsafe) |
0.9 |
1.7 |
This cop enforces the use of consistent method names from the Enumerable module.
You can customize the mapping from undesired method to desired method.
e.g. to use detect
over find
:
Style/CollectionMethods: PreferredMethods: find: detect
Safety
This cop is unsafe because it finds methods by name, without actually being able to determine if the receiver is an Enumerable or not, so this cop may register false positives.
Examples
# These examples are based on the default mapping for `PreferredMethods`.
# bad
items.collect
items.collect!
items.inject
items.detect
items.find_all
items.member?
# good
items.map
items.map!
items.reduce
items.find
items.select
items.include?
Style/ColonMethodCall
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.9 |
- |
- This cop checks for methods invoked via the
-
operator instead of the . operator (like FileUtils::rmdir instead of FileUtils.rmdir).
Examples
# bad
Timeout::timeout(500) { do_something }
FileUtils::rmdir(dir)
Marshal::dump(obj)
# good
Timeout.timeout(500) { do_something }
FileUtils.rmdir(dir)
Marshal.dump(obj)
Style/ColonMethodDefinition
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.52 |
- |
This cop checks for class methods that are defined using the ::
operator instead of the .
operator.
Style/CombinableLoops
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
No |
No |
0.90 |
- |
This cop checks for places where multiple consecutive loops over the same data can be combined into a single loop. It is very likely that combining them will make the code more efficient and more concise.
Safety
The cop is unsafe, because the first loop might modify state that the second loop depends on; these two aren’t combinable.
Examples
# bad
def method
items.each do |item|
do_something(item)
end
items.each do |item|
do_something_else(item)
end
end
# good
def method
items.each do |item|
do_something(item)
do_something_else(item)
end
end
# bad
def method
for item in items do
do_something(item)
end
for item in items do
do_something_else(item)
end
end
# good
def method
for item in items do
do_something(item)
do_something_else(item)
end
end
# good
def method
each_slice(2) { |slice| do_something(slice) }
each_slice(3) { |slice| do_something(slice) }
end
Style/CommandLiteral
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.30 |
- |
This cop enforces using `` or %x around command literals.
Examples
EnforcedStyle: backticks (default)
# bad
folders = %x(find . -type d).split
# bad
%x(
ln -s foo.example.yml foo.example
ln -s bar.example.yml bar.example
)
# good
folders = `find . -type d`.split
# good
`
ln -s foo.example.yml foo.example
ln -s bar.example.yml bar.example
`
EnforcedStyle: mixed
# bad
folders = %x(find . -type d).split
# bad
`
ln -s foo.example.yml foo.example
ln -s bar.example.yml bar.example
`
# good
folders = `find . -type d`.split
# good
%x(
ln -s foo.example.yml foo.example
ln -s bar.example.yml bar.example
)
EnforcedStyle: percent_x
# bad
folders = `find . -type d`.split
# bad
`
ln -s foo.example.yml foo.example
ln -s bar.example.yml bar.example
`
# good
folders = %x(find . -type d).split
# good
%x(
ln -s foo.example.yml foo.example
ln -s bar.example.yml bar.example
)
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle |
|
|
AllowInnerBackticks |
|
Boolean |
Style/CommentAnnotation
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.10 |
1.20 |
This cop checks that comment annotation keywords are written according to guidelines.
Annotation keywords can be specified by overriding the cop’s Keywords
configuration. Keywords are allowed to be single words or phrases.
With a multiline comment block (where each line is only a
comment), only the first line will be able to register an offense, even
if an annotation keyword starts another line. This is done to prevent
incorrect registering of keywords (eg. review ) inside a paragraph as an
annotation.
|
Examples
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
Keywords |
|
Array |
RequireColon |
|
Boolean |
Style/CommentedKeyword
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes (Unsafe) |
0.51 |
1.19 |
This cop checks for comments put on the same line as some keywords.
These keywords are: class
, module
, def
, begin
, end
.
Note that some comments
(:nodoc:
, :yields:
, rubocop:disable
and rubocop:todo
)
are allowed.
Auto-correction removes comments from end
keyword and keeps comments
for class
, module
, def
and begin
above the keyword.
Style/ConditionalAssignment
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.36 |
0.47 |
Check for if
and case
statements where each branch is used for
assignment to the same variable when using the return of the
condition can be used instead.
Examples
EnforcedStyle: assign_to_condition (default)
# bad
if foo
bar = 1
else
bar = 2
end
case foo
when 'a'
bar += 1
else
bar += 2
end
if foo
some_method
bar = 1
else
some_other_method
bar = 2
end
# good
bar = if foo
1
else
2
end
bar += case foo
when 'a'
1
else
2
end
bar << if foo
some_method
1
else
some_other_method
2
end
EnforcedStyle: assign_inside_condition
# bad
bar = if foo
1
else
2
end
bar += case foo
when 'a'
1
else
2
end
bar << if foo
some_method
1
else
some_other_method
2
end
# good
if foo
bar = 1
else
bar = 2
end
case foo
when 'a'
bar += 1
else
bar += 2
end
if foo
some_method
bar = 1
else
some_other_method
bar = 2
end
Style/ConstantVisibility
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Disabled |
Yes |
No |
0.66 |
1.10 |
This cop checks that constants defined in classes and modules have an explicit visibility declaration. By default, Ruby makes all class- and module constants public, which litters the public API of the class or module. Explicitly declaring a visibility makes intent more clear, and prevents outside actors from touching private state.
Style/Copyright
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Disabled |
Yes |
Yes |
0.30 |
- |
Check that a copyright notice was given in each source file.
The default regexp for an acceptable copyright notice can be found in config/default.yml. The default can be changed as follows:
Style/Copyright: Notice: '^Copyright (\(c\) )?2\d{3} Acme Inc'
This regex string is treated as an unanchored regex. For each file that RuboCop scans, a comment that matches this regex must be found or an offense is reported.
Style/DateTime
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Disabled |
Yes |
Yes (Unsafe) |
0.51 |
0.92 |
This cop checks for consistent usage of the DateTime
class over the
Time
class. This cop is disabled by default since these classes,
although highly overlapping, have particularities that make them not
replaceable in certain situations when dealing with multiple timezones
and/or DST.
Safety
Autocorrection is not safe, because DateTime
and Time
do not have
exactly the same behaviour, although in most cases the autocorrection
will be fine.
Examples
# bad - uses `DateTime` for current time
DateTime.now
# good - uses `Time` for current time
Time.now
# bad - uses `DateTime` for modern date
DateTime.iso8601('2016-06-29')
# good - uses `Time` for modern date
Time.iso8601('2016-06-29')
# good - uses `DateTime` with start argument for historical date
DateTime.iso8601('1751-04-23', Date::ENGLAND)
Style/DefWithParentheses
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.9 |
0.12 |
This cop checks for parentheses in the definition of a method, that does not take any arguments. Both instance and class/singleton methods are checked.
Examples
# bad
def foo()
# does a thing
end
# good
def foo
# does a thing
end
# also good
def foo() does_a_thing end
# bad
def Baz.foo()
# does a thing
end
# good
def Baz.foo
# does a thing
end
Style/Dir
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.50 |
- |
This cop checks for places where the #dir
method can replace more
complex constructs to retrieve a canonicalized absolute path to the
current file.
Style/DisableCopsWithinSourceCodeDirective
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Disabled |
Yes |
Yes |
0.82 |
1.9 |
Detects comments to enable/disable RuboCop. This is useful if want to make sure that every RuboCop error gets fixed and not quickly disabled with a comment.
Specific cops can be allowed with the AllowedCops
configuration. Note that
if this configuration is set, rubocop:disable all
is still disallowed.
Style/DocumentDynamicEvalDefinition
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Pending |
Yes |
No |
1.1 |
1.3 |
When using class_eval
(or other eval
) with string interpolation,
add a comment block showing its appearance if interpolated (a practice used in Rails code).
Examples
# from activesupport/lib/active_support/core_ext/string/output_safety.rb
# bad
UNSAFE_STRING_METHODS.each do |unsafe_method|
if 'String'.respond_to?(unsafe_method)
class_eval <<-EOT, __FILE__, __LINE__ + 1
def #{unsafe_method}(*params, &block)
to_str.#{unsafe_method}(*params, &block)
end
def #{unsafe_method}!(*params)
@dirty = true
super
end
EOT
end
end
# good, inline comments in heredoc
UNSAFE_STRING_METHODS.each do |unsafe_method|
if 'String'.respond_to?(unsafe_method)
class_eval <<-EOT, __FILE__, __LINE__ + 1
def #{unsafe_method}(*params, &block) # def capitalize(*params, &block)
to_str.#{unsafe_method}(*params, &block) # to_str.capitalize(*params, &block)
end # end
def #{unsafe_method}!(*params) # def capitalize!(*params)
@dirty = true # @dirty = true
super # super
end # end
EOT
end
end
# good, block comments in heredoc
class_eval <<-EOT, __FILE__, __LINE__ + 1
# def capitalize!(*params)
# @dirty = true
# super
# end
def #{unsafe_method}!(*params)
@dirty = true
super
end
EOT
# good, block comments before heredoc
class_eval(
# def capitalize!(*params)
# @dirty = true
# super
# end
<<-EOT, __FILE__, __LINE__ + 1
def #{unsafe_method}!(*params)
@dirty = true
super
end
EOT
)
# bad - interpolated string without comment
class_eval("def #{unsafe_method}!(*params); end")
# good - with inline comment or replace it with block comment using heredoc
class_eval("def #{unsafe_method}!(*params); end # def capitalize!(*params); end")
Style/Documentation
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
No |
0.9 |
- |
This cop checks for missing top-level documentation of classes and modules. Classes with no body are exempt from the check and so are namespace modules - modules that have nothing in their bodies except classes, other modules, constant definitions or constant visibility declarations.
The documentation requirement is annulled if the class or module has a ":nodoc:" comment next to it. Likewise, ":nodoc: all" does the same for all its children.
Examples
# bad
class Person
# ...
end
module Math
end
# good
# Description/Explanation of Person class
class Person
# ...
end
# allowed
# Class without body
class Person
end
# Namespace - A namespace can be a class or a module
# Containing a class
module Namespace
# Description/Explanation of Person class
class Person
# ...
end
end
# Containing constant visibility declaration
module Namespace
class Private
end
private_constant :Private
end
# Containing constant definition
module Namespace
Public = Class.new
end
# Macro calls
module Namespace
extend Foo
end
Style/DocumentationMethod
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Disabled |
Yes |
No |
0.43 |
- |
This cop checks for missing documentation comment for public methods. It can optionally be configured to also require documentation for non-public methods.
Examples
# bad
class Foo
def bar
puts baz
end
end
module Foo
def bar
puts baz
end
end
def foo.bar
puts baz
end
# good
class Foo
# Documentation
def bar
puts baz
end
end
module Foo
# Documentation
def bar
puts baz
end
end
# Documentation
def foo.bar
puts baz
end
Style/DoubleCopDisableDirective
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.73 |
- |
Detects double disable comments on one line. This is mostly to catch automatically generated comments that need to be regenerated.
Style/DoubleNegation
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes (Unsafe) |
0.19 |
1.2 |
This cop checks for uses of double negation (!!
) to convert something to a boolean value.
When using EnforcedStyle: allowed_in_returns
, allow double negation in contexts
that use boolean as a return value. When using EnforcedStyle: forbidden
, double negation
should be forbidden always.
when something is a boolean value
!!something and !something.nil? are not the same thing.
As you’re unlikely to write code that can accept values of any type
this is rarely a problem in practice.
|
Safety
Autocorrection is unsafe when the value is false
, because the result
of the expression will change.
!!false #=> false
!false.nil? #=> true
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle |
|
|
Style/EachForSimpleLoop
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.41 |
- |
This cop checks for loops which iterate a constant number of times,
using a Range literal and #each
. This can be done more readably using
Integer#times
.
This check only applies if the block takes no parameters.
Style/EachWithObject
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.22 |
0.42 |
This cop looks for inject / reduce calls where the passed in object is returned at the end and so could be replaced by each_with_object without the need to return the object at the end.
However, we can’t replace with each_with_object if the accumulator parameter is assigned to within the block.
Style/EmptyBlockParameter
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.52 |
- |
This cop checks for pipes for empty block parameters. Pipes for empty block parameters do not cause syntax errors, but they are redundant.
Style/EmptyCaseCondition
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.40 |
- |
This cop checks for case statements with an empty condition.
Examples
# bad:
case
when x == 0
puts 'x is 0'
when y == 0
puts 'y is 0'
else
puts 'neither is 0'
end
# good:
if x == 0
puts 'x is 0'
elsif y == 0
puts 'y is 0'
else
puts 'neither is 0'
end
# good: (the case condition node is not empty)
case n
when 0
puts 'zero'
when 1
puts 'one'
else
puts 'more'
end
Style/EmptyElse
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.28 |
0.32 |
Checks for empty else-clauses, possibly including comments and/or an
explicit nil
depending on the EnforcedStyle.
Examples
EnforcedStyle: empty
# warn only on empty else
# bad
if condition
statement
else
end
# good
if condition
statement
else
nil
end
# good
if condition
statement
else
statement
end
# good
if condition
statement
end
Style/EmptyLambdaParameter
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.52 |
- |
This cop checks for parentheses for empty lambda parameters. Parentheses for empty lambda parameters do not cause syntax errors, but they are redundant.
Style/EmptyLiteral
Style/EmptyMethod
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.46 |
- |
This cop checks for the formatting of empty method definitions.
By default it enforces empty method definitions to go on a single
line (compact style), but it can be configured to enforce the end
to go on its own line (expanded style).
A method definition is not considered empty if it contains comments. |
Examples
Style/Encoding
Style/EndBlock
Style/EndlessMethod
Required Ruby version: 3.0 |
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Pending |
Yes |
Yes |
1.8 |
- |
This cop checks for endless methods.
It can enforce either the use of endless methods definitions for single-lined method bodies, or disallow endless methods.
Other method definition types are not considered by this cop.
The supported styles are:
-
allow_single_line (default) - only single line endless method definitions are allowed.
-
allow_always - all endless method definitions are allowed.
-
disallow - all endless method definitions are disallowed.
Incorrect endless method definitions will always be corrected to a multi-line definition. |
Examples
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle |
|
|
Style/EvalWithLocation
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.52 |
- |
This cop ensures that eval methods (eval
, instance_eval
, class_eval
and module_eval
) are given filename and line number values (FILE
and LINE
). This data is used to ensure that any errors raised
within the evaluated code will be given the correct identification
in a backtrace.
The cop also checks that the line number given relative to LINE
is
correct.
This cop will autocorrect incorrect or missing filename and line number
values. However, if eval
is called without a binding argument, the cop
will not attempt to automatically add a binding, or add filename and
line values.
This cop works only when a string literal is given as a code string. No offense is reported if a string variable is given as below:
Examples
# bad
eval <<-RUBY
def do_something
end
RUBY
# bad
C.class_eval <<-RUBY
def do_something
end
RUBY
# good
eval <<-RUBY, binding, __FILE__, __LINE__ + 1
def do_something
end
RUBY
# good
C.class_eval <<-RUBY, __FILE__, __LINE__ + 1
def do_something
end
RUBY
# not checked
code = <<-RUBY
def do_something
end
RUBY
eval code
Style/EvenOdd
Style/ExpandPathArguments
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.53 |
- |
This cop checks for use of the File.expand_path
arguments.
Likewise, it also checks for the Pathname.new
argument.
Contrastive bad case and good case are alternately shown in the following examples.
Examples
# bad
File.expand_path('..', __FILE__)
# good
File.expand_path(__dir__)
# bad
File.expand_path('../..', __FILE__)
# good
File.expand_path('..', __dir__)
# bad
File.expand_path('.', __FILE__)
# good
File.expand_path(__FILE__)
# bad
Pathname(__FILE__).parent.expand_path
# good
Pathname(__dir__).expand_path
# bad
Pathname.new(__FILE__).parent.expand_path
# good
Pathname.new(__dir__).expand_path
Style/ExplicitBlockArgument
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.89 |
1.8 |
This cop enforces the use of explicit block argument to avoid writing block literal that just passes its arguments to another block.
This cop only registers an offense if the block args match the yield args exactly. |
Examples
# bad
def with_tmp_dir
Dir.mktmpdir do |tmp_dir|
Dir.chdir(tmp_dir) { |dir| yield dir } # block just passes arguments
end
end
# bad
def nine_times
9.times { yield }
end
# good
def with_tmp_dir(&block)
Dir.mktmpdir do |tmp_dir|
Dir.chdir(tmp_dir, &block)
end
end
with_tmp_dir do |dir|
puts "dir is accessible as a parameter and pwd is set: #{dir}"
end
# good
def nine_times(&block)
9.times(&block)
end
Style/ExponentialNotation
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
No |
0.82 |
- |
This cop enforces consistency when using exponential notation for numbers in the code (eg 1.2e4). Different styles are supported:
-
scientific
which enforces a mantissa between 1 (inclusive) and 10 (exclusive). -
engineering
which enforces the exponent to be a multiple of 3 and the mantissa to be between 0.1 (inclusive) and 10 (exclusive). -
integral
which enforces the mantissa to always be a whole number without trailing zeroes.
Examples
EnforcedStyle: scientific (default)
# Enforces a mantissa between 1 (inclusive) and 10 (exclusive).
# bad
10e6
0.3e4
11.7e5
3.14e0
# good
1e7
3e3
1.17e6
3.14
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle |
|
|
Style/FileRead
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Pending |
Yes |
Yes |
1.24 |
- |
Favor File.(bin)read
convenience methods.
Examples
## text mode
# bad
File.open(filename).read
File.open(filename, &:read)
File.open(filename) { |f| f.read }
File.open(filename) do |f|
f.read
end
File.open(filename, 'r').read
File.open(filename, 'r', &:read)
File.open(filename, 'r') do |f|
f.read
end
# good
File.read(filename)
## binary mode
# bad
File.open(filename, 'rb').read
File.open(filename, 'rb', &:read)
File.open(filename, 'rb') do |f|
f.read
end
# good
File.binread(filename)
Style/FileWrite
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Pending |
Yes |
Yes |
1.24 |
- |
Favor File.(bin)write
convenience methods.
Examples
## text mode
# bad
File.open(filename, 'w').write(content)
File.open(filename, 'w') do |f|
f.write(content)
end
# good
File.write(filename, content)
## binary mode
# bad
File.open(filename, 'wb').write(content)
File.open(filename, 'wb') do |f|
f.write(content)
end
# good
File.binwrite(filename, content)
Style/FloatDivision
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
No |
Yes (Unsafe) |
0.72 |
1.9 |
This cop checks for division with integers coerced to floats.
It is recommended to either always use fdiv
or coerce one side only.
This cop also provides other options for code consistency.
Safety
This cop is unsafe, because if the operand variable is a string object
then .to_f
will be removed and an error will occur.
a = '1.2'
b = '3.4'
a.to_f / b.to_f # Both `to_f` calls are required here
Style/For
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.13 |
0.59 |
This cop looks for uses of the for
keyword or each
method. The
preferred alternative is set in the EnforcedStyle configuration
parameter. An each
call with a block on a single line is always
allowed.
Examples
Style/FormatString
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.19 |
0.49 |
This cop enforces the use of a single string formatting utility. Valid options include Kernel#format, Kernel#sprintf and String#%.
The detection of String#% cannot be implemented in a reliable manner for all cases, so only two scenarios are considered - if the first argument is a string literal and if the second argument is an array literal.
Examples
EnforcedStyle: format (default)
# bad
puts sprintf('%10s', 'hoge')
puts '%10s' % 'hoge'
# good
puts format('%10s', 'hoge')
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle |
|
|
Style/FormatStringToken
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
No |
0.49 |
1.0 |
Use a consistent style for named format string tokens.
unannotated style cop only works for strings
which are passed as arguments to those methods:
printf , sprintf , format , % .
The reason is that unannotated format is very similar
to encoded URLs or Date/Time formatting strings.
|
This cop can be customized ignored methods with IgnoredMethods
.
It is allowed to contain unannotated token
if the number of them is less than or equals to
MaxUnannotatedPlaceholdersAllowed
.
Examples
EnforcedStyle: annotated (default)
# bad
format('%{greeting}', greeting: 'Hello')
format('%s', 'Hello')
# good
format('%<greeting>s', greeting: 'Hello')
EnforcedStyle: template
# bad
format('%<greeting>s', greeting: 'Hello')
format('%s', 'Hello')
# good
format('%{greeting}', greeting: 'Hello')
EnforcedStyle: unannotated
# bad
format('%<greeting>s', greeting: 'Hello')
format('%{greeting}', greeting: 'Hello')
# good
format('%s', 'Hello')
MaxUnannotatedPlaceholdersAllowed: 0
# bad
format('%06d', 10)
format('%s %s.', 'Hello', 'world')
# good
format('%<number>06d', number: 10)
Style/FrozenStringLiteralComment
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes (Unsafe) |
0.36 |
0.79 |
This cop is designed to help you transition from mutable string literals
to frozen string literals.
It will add the # frozen_string_literal: true
magic comment to the top
of files to enable frozen string literals. Frozen string literals may be
default in future Ruby. The comment will be added below a shebang and
encoding comment.
Note that the cop will accept files where the comment exists but is set
to false
instead of true
.
To require a blank line after this comment, please see
Layout/EmptyLineAfterMagicComment
cop.
Safety
This cop’s autocorrection is unsafe since any strings mutations will
change from being accepted to raising FrozenError
, as all strings
will become frozen by default, and will need to be manually refactored.
Examples
EnforcedStyle: always (default)
# The `always` style will always add the frozen string literal comment
# to a file, regardless of the Ruby version or if `freeze` or `<<` are
# called on a string literal.
# bad
module Bar
# ...
end
# good
# frozen_string_literal: true
module Bar
# ...
end
# good
# frozen_string_literal: false
module Bar
# ...
end
EnforcedStyle: never
# The `never` will enforce that the frozen string literal comment does
# not exist in a file.
# bad
# frozen_string_literal: true
module Baz
# ...
end
# good
module Baz
# ...
end
EnforcedStyle: always_true
# The `always_true` style enforces that the frozen string literal
# comment is set to `true`. This is a stricter option than `always`
# and forces projects to use frozen string literals.
# bad
# frozen_string_literal: false
module Baz
# ...
end
# bad
module Baz
# ...
end
# good
# frozen_string_literal: true
module Bar
# ...
end
Style/GlobalStdStream
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes (Unsafe) |
0.89 |
- |
This cop enforces the use of $stdout/$stderr/$stdin
instead of STDOUT/STDERR/STDIN
.
STDOUT/STDERR/STDIN
are constants, and while you can actually
reassign (possibly to redirect some stream) constants in Ruby, you’ll get
an interpreter warning if you do so.
Safety
Autocorrection is unsafe because STDOUT
and $stdout
may point to different
objects, for example.
Examples
# bad
STDOUT.puts('hello')
hash = { out: STDOUT, key: value }
def m(out = STDOUT)
out.puts('hello')
end
# good
$stdout.puts('hello')
hash = { out: $stdout, key: value }
def m(out = $stdout)
out.puts('hello')
end
Style/GlobalVars
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
No |
0.13 |
- |
This cop looks for uses of global variables. It does not report offenses for built-in global variables. Built-in global variables are allowed by default. Additionally users can allow additional variables via the AllowedVariables option.
Note that backreferences like $1, $2, etc are not global variables.
Style/GuardClause
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
No |
0.20 |
0.22 |
Use a guard clause instead of wrapping the code inside a conditional expression
Examples
# bad
def test
if something
work
end
end
# good
def test
return unless something
work
end
# also good
def test
work if something
end
# bad
if something
raise 'exception'
else
ok
end
# good
raise 'exception' if something
ok
# bad
if something
foo || raise('exception')
else
ok
end
# good
foo || raise('exception') if something
ok
Style/HashAsLastArrayItem
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.88 |
- |
Checks for presence or absence of braces around hash literal as a last array item depending on configuration.
This cop will ignore arrays where all items are hashes, regardless of EnforcedStyle. |
Examples
Style/HashConversion
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Pending |
Yes |
Yes |
1.10 |
1.11 |
This cop checks the usage of pre-2.1 Hash[args]
method of converting enumerables and
sequences of values to hashes.
Correction code from splat argument (Hash[*ary]
) is not simply determined. For example,
Hash[*ary]
can be replaced with ary.each_slice(2).to_h
but it will be complicated.
So, AllowSplatArgument
option is true by default to allow splat argument for simple code.
Style/HashEachMethods
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
No |
Yes (Unsafe) |
0.80 |
1.16 |
This cop checks for uses of each_key
and each_value
Hash methods.
If you have an array of two-element arrays, you can put parentheses around the block arguments to indicate that you’re not working with a hash, and suppress RuboCop offenses. |
Safety
This cop is unsafe because it cannot be guaranteed that the receiver
is a Hash
. The AllowedReceivers
configuration can mitigate,
but not fully resolve, this safety issue.
Examples
# bad
hash.keys.each { |k| p k }
hash.values.each { |v| p v }
# good
hash.each_key { |k| p k }
hash.each_value { |v| p v }
Style/HashExcept
Required Ruby version: 3.0 |
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Pending |
Yes |
Yes |
1.7 |
- |
This cop checks for usages of Hash#reject
, Hash#select
, and Hash#filter
methods
that can be replaced with Hash#except
method.
This cop should only be enabled on Ruby version 3.0 or higher.
(Hash#except
was added in Ruby 3.0.)
For safe detection, it is limited to commonly used string and symbol comparisons
when used ==
.
And do not check Hash#delete_if
and Hash#keep_if
to change receiver object.
Style/HashLikeCase
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
No |
0.88 |
- |
This cop checks for places where case-when
represents a simple 1:1
mapping and can be replaced with a hash lookup.
Examples
MinBranchesCount: 3 (default)
# bad
case country
when 'europe'
'http://eu.example.com'
when 'america'
'http://us.example.com'
when 'australia'
'http://au.example.com'
end
# good
SITES = {
'europe' => 'http://eu.example.com',
'america' => 'http://us.example.com',
'australia' => 'http://au.example.com'
}
SITES[country]
Style/HashSyntax
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.9 |
1.24 |
This cop checks hash literal syntax.
It can enforce either the use of the class hash rocket syntax or the use of the newer Ruby 1.9 syntax (when applicable).
A separate offense is registered for each problematic pair.
The supported styles are:
-
ruby19 - forces use of the 1.9 syntax (e.g.
{a: 1}
) when hashes have all symbols for keys -
hash_rockets - forces use of hash rockets for all hashes
-
no_mixed_keys - simply checks for hashes with mixed syntaxes
-
ruby19_no_mixed_keys - forces use of ruby 1.9 syntax and forbids mixed syntax hashes
This cop has EnforcedShorthandSyntax
option.
It can enforce either the use of the explicit hash value syntax or
the use of Ruby 3.1’s hash value shorthand syntax.
The supported styles are:
-
always - forces use of the 3.1 syntax (e.g. {foo:})
-
never - forces use of explicit hash literal value
-
either - accepts both shorthand and explicit use of hash literal value
Examples
EnforcedStyle: ruby19 (default)
# bad
{:a => 2}
{b: 1, :c => 2}
# good
{a: 2, b: 1}
{:c => 2, 'd' => 2} # acceptable since 'd' isn't a symbol
{d: 1, 'e' => 2} # technically not forbidden
EnforcedStyle: no_mixed_keys
# bad
{:a => 1, b: 2}
{c: 1, 'd' => 2}
# good
{:a => 1, :b => 2}
{c: 1, d: 2}
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle |
|
|
EnforcedShorthandSyntax |
|
|
UseHashRocketsWithSymbolValues |
|
Boolean |
PreferHashRocketsForNonAlnumEndingSymbols |
|
Boolean |
Style/HashTransformKeys
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
No |
Yes (Unsafe) |
0.80 |
0.90 |
This cop looks for uses of .each_with_object({}) {…}
,
.map {…}.to_h
, and Hash[_.map {…}]
that are actually just
transforming the keys of a hash, and tries to use a simpler & faster
call to transform_keys
instead.
Safety
This cop is unsafe, as it can produce false positives if we are
transforming an enumerable of key-value-like pairs that isn’t actually
a hash, e.g.: [[k1, v1], [k2, v2], …]
Examples
# bad
{a: 1, b: 2}.each_with_object({}) { |(k, v), h| h[foo(k)] = v }
Hash[{a: 1, b: 2}.collect { |k, v| [foo(k), v] }]
{a: 1, b: 2}.map { |k, v| [k.to_s, v] }.to_h
{a: 1, b: 2}.to_h { |k, v| [k.to_s, v] }
# good
{a: 1, b: 2}.transform_keys { |k| foo(k) }
{a: 1, b: 2}.transform_keys { |k| k.to_s }
Style/HashTransformValues
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
No |
Yes (Unsafe) |
0.80 |
0.90 |
This cop looks for uses of .each_with_object({}) {…}
,
.map {…}.to_h
, and Hash[_.map {…}]
that are actually just
transforming the values of a hash, and tries to use a simpler & faster
call to transform_values
instead.
Safety
This cop is unsafe, as it can produce false positives if we are
transforming an enumerable of key-value-like pairs that isn’t actually
a hash, e.g.: [[k1, v1], [k2, v2], …]
Examples
# bad
{a: 1, b: 2}.each_with_object({}) { |(k, v), h| h[k] = foo(v) }
Hash[{a: 1, b: 2}.collect { |k, v| [k, foo(v)] }]
{a: 1, b: 2}.map { |k, v| [k, v * v] }.to_h
{a: 1, b: 2}.to_h { |k, v| [k, v * v] }
# good
{a: 1, b: 2}.transform_values { |v| foo(v) }
{a: 1, b: 2}.transform_values { |v| v * v }
Style/IdenticalConditionalBranches
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes (Unsafe) |
0.36 |
1.19 |
This cop checks for identical expressions at the beginning or end of each branch of a conditional expression. Such expressions should normally be placed outside the conditional expression - before or after it.
The cop is poorly named and some people might think that it actually checks for duplicated conditional branches. The name will probably be changed in a future major RuboCop release. |
Safety
Auto-correction is unsafe because changing the order of method invocations may change the behaviour of the code. For example:
if method_that_modifies_global_state # 1
method_that_relies_on_global_state # 2
foo # 3
else
method_that_relies_on_global_state # 2
bar # 3
end
In this example, method_that_relies_on_global_state
will be moved before
method_that_modifies_global_state
, which changes the behaviour of the program.
Examples
# bad
if condition
do_x
do_z
else
do_y
do_z
end
# good
if condition
do_x
else
do_y
end
do_z
# bad
if condition
do_z
do_x
else
do_z
do_y
end
# good
do_z
if condition
do_x
else
do_y
end
# bad
case foo
when 1
do_x
when 2
do_x
else
do_x
end
# good
case foo
when 1
do_x
do_y
when 2
# nothing
else
do_x
do_z
end
# bad
case foo
in 1
do_x
in 2
do_x
else
do_x
end
# good
case foo
in 1
do_x
do_y
in 2
# nothing
else
do_x
do_z
end
Style/IfInsideElse
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.36 |
1.3 |
If the else
branch of a conditional consists solely of an if
node,
it can be combined with the else
to become an elsif
.
This helps to keep the nesting level from getting too deep.
Examples
# bad
if condition_a
action_a
else
if condition_b
action_b
else
action_c
end
end
# good
if condition_a
action_a
elsif condition_b
action_b
else
action_c
end
Style/IfUnlessModifier
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.9 |
0.30 |
Checks for if
and unless
statements that would fit on one line if
written as modifier if
/unless
. The cop also checks for modifier
if
/unless
lines that exceed the maximum line length.
The maximum line length is configured in the Layout/LineLength
cop. The tab size is configured in the IndentationWidth
of the
Layout/IndentationStyle
cop.
Examples
# bad
if condition
do_stuff(bar)
end
unless qux.empty?
Foo.do_something
end
do_something_with_a_long_name(arg) if long_condition_that_prevents_code_fit_on_single_line
# good
do_stuff(bar) if condition
Foo.do_something unless qux.empty?
if long_condition_that_prevents_code_fit_on_single_line
do_something_with_a_long_name(arg)
end
if short_condition # a long comment that makes it too long if it were just a single line
do_something
end
Style/IfUnlessModifierOfIfUnless
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.39 |
0.87 |
Checks for if and unless statements used as modifiers of other if or unless statements.
Style/IfWithBooleanLiteralBranches
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Pending |
Yes |
Yes (Unsafe) |
1.9 |
- |
This cop checks for redundant if
with boolean literal branches.
It checks only conditions to return boolean value (true
or false
) for safe detection.
The conditions to be checked are comparison methods, predicate methods, and double negative.
Safety
Auto-correction is unsafe because there is no guarantee that all predicate methods
will return a boolean value. Those methods can be allowed with AllowedMethods
config.
Style/IfWithSemicolon
Style/ImplicitRuntimeError
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Disabled |
Yes |
No |
0.41 |
- |
This cop checks for raise
or fail
statements which do not specify an
explicit exception class. (This raises a RuntimeError
. Some projects
might prefer to use exception classes which more precisely identify the
nature of the error.)
Style/InPatternThen
Required Ruby version: 2.7 |
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Pending |
Yes |
Yes |
1.16 |
- |
This cop checks for in;
uses in case
expressions.
Style/InfiniteLoop
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
No |
Yes (Unsafe) |
0.26 |
0.61 |
Use Kernel#loop
for infinite loops.
Safety
This cop is unsafe as the rule should not necessarily apply if the loop
body might raise a StopIteration
exception; contrary to other infinite
loops, Kernel#loop
silently rescues that and returns nil
.
Style/InverseMethods
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
No |
Yes (Unsafe) |
0.48 |
- |
This cop check for usages of not (not
or !
) called on a method
when an inverse of that method can be used instead.
Methods that can be inverted by a not (not
or !
) should be defined
in InverseMethods
.
Methods that are inverted by inverting the return
of the block that is passed to the method should be defined in
InverseBlocks
.
Safety
This cop is unsafe because it cannot be guaranteed that the method and its inverse method are both defined on receiver, and also are actually inverse of each other.
Examples
# bad
!foo.none?
!foo.any? { |f| f.even? }
!foo.blank?
!(foo == bar)
foo.select { |f| !f.even? }
foo.reject { |f| f != 7 }
# good
foo.none?
foo.blank?
foo.any? { |f| f.even? }
foo != bar
foo == bar
!!('foo' =~ /^\w+$/)
!(foo.class < Numeric) # Checking class hierarchy is allowed
# Blocks with guard clauses are ignored:
foo.select do |f|
next if f.zero?
f != 1
end
Style/IpAddresses
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Disabled |
Yes |
No |
0.58 |
0.91 |
This cop checks for hardcoded IP addresses, which can make code brittle. IP addresses are likely to need to be changed when code is deployed to a different server or environment, which may break a deployment if forgotten. Prefer setting IP addresses in ENV or other configuration.
Style/KeywordParametersOrder
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.90 |
1.7 |
This cop enforces that optional keyword parameters are placed at the end of the parameters list.
This improves readability, because when looking through the source, it is expected to find required parameters at the beginning of parameters list and optional parameters at the end.
Examples
# bad
def some_method(first: false, second:, third: 10)
# body omitted
end
# good
def some_method(second:, first: false, third: 10)
# body omitted
end
# bad
do_something do |first: false, second:, third: 10|
# body omitted
end
# good
do_something do |second:, first: false, third: 10|
# body omitted
end
Style/Lambda
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.9 |
0.40 |
This cop (by default) checks for uses of the lambda literal syntax for single line lambdas, and the method call syntax for multiline lambdas. It is configurable to enforce one of the styles for both single line and multiline lambdas as well.
Examples
EnforcedStyle: line_count_dependent (default)
# bad
f = lambda { |x| x }
f = ->(x) do
x
end
# good
f = ->(x) { x }
f = lambda do |x|
x
end
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle |
|
|
Style/LambdaCall
Style/LineEndConcatenation
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes (Unsafe) |
0.18 |
0.64 |
This cop checks for string literal concatenation at the end of a line.
Style/MapToHash
Required Ruby version: 2.6 |
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Pending |
No |
Yes (Unsafe) |
1.24 |
- |
This cop looks for uses of map.to_h
or collect.to_h
that could be
written with just to_h
in Ruby >= 2.6.
Style/HashTransformKeys and Style/HashTransformValues will
also change this pattern if only hash keys or hash values are being
transformed.
|
Style/MethodCallWithArgsParentheses
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Disabled |
Yes |
Yes |
0.47 |
1.7 |
This cop enforces the presence (default) or absence of parentheses in method calls containing parameters.
In the default style (require_parentheses), macro methods are ignored.
Additional methods can be added to the IgnoredMethods
or IgnoredPatterns
list. These options are
valid only in the default style. Macros can be included by
either setting IgnoreMacros
to false or adding specific macros to
the IncludedMacros
list.
Precedence of options is all follows:
-
IgnoredMethods
-
IgnoredPatterns
-
IncludedMacros
eg. If a method is listed in both
IncludedMacros
and IgnoredMethods
, then the latter takes
precedence (that is, the method is ignored).
In the alternative style (omit_parentheses), there are three additional options.
-
AllowParenthesesInChaining
isfalse
by default. Setting it totrue
allows the presence of parentheses in the last call during method chaining. -
AllowParenthesesInMultilineCall
isfalse
by default. Setting it totrue
allows the presence of parentheses in multi-line method calls. -
AllowParenthesesInCamelCaseMethod
isfalse
by default. This allows the presence of parentheses when calling a method whose name begins with a capital letter and which has no arguments. Setting it totrue
allows the presence of parentheses in such a method call even with arguments.
Parentheses are still allowed in cases where omitting them results in ambiguous or syntactically incorrect code. For example, parentheses are required around a method with arguments when inside an endless method definition introduced in Ruby 3.0. Parentheses are also allowed when forwarding arguments with the triple-dot syntax introduced in Ruby 2.7 as omitting them starts an endless range. And Ruby 3.1’s hash omission syntax has a case that requires parentheses because of the following issue: https://bugs.ruby-lang.org/issues/18396. |
Examples
EnforcedStyle: require_parentheses (default)
# bad
array.delete e
# good
array.delete(e)
# good
# Operators don't need parens
foo == bar
# good
# Setter methods don't need parens
foo.bar = baz
# okay with `puts` listed in `IgnoredMethods`
puts 'test'
# okay with `^assert` listed in `IgnoredPatterns`
assert_equal 'test', x
EnforcedStyle: omit_parentheses
# bad
array.delete(e)
# good
array.delete e
# bad
foo.enforce(strict: true)
# good
foo.enforce strict: true
# good
# Allows parens for calls that won't produce valid Ruby or be ambiguous.
model.validate strict(true)
# good
# Allows parens for calls that won't produce valid Ruby or be ambiguous.
yield path, File.basename(path)
# good
# Operators methods calls with parens
array&.[](index)
# good
# Operators methods without parens, if you prefer
array.[] index
# good
# Operators methods calls with parens
array&.[](index)
# good
# Operators methods without parens, if you prefer
array.[] index
AllowParenthesesInMultilineCall: false (default)
# bad
foo.enforce(
strict: true
)
# good
foo.enforce \
strict: true
AllowParenthesesInMultilineCall: true
# good
foo.enforce(
strict: true
)
# good
foo.enforce \
strict: true
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
IgnoreMacros |
|
Boolean |
IgnoredMethods |
|
Array |
IgnoredPatterns |
|
Array |
IncludedMacros |
|
Array |
AllowParenthesesInMultilineCall |
|
Boolean |
AllowParenthesesInChaining |
|
Boolean |
AllowParenthesesInCamelCaseMethod |
|
Boolean |
AllowParenthesesInStringInterpolation |
|
Boolean |
EnforcedStyle |
|
|
Style/MethodCallWithoutArgsParentheses
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.47 |
0.55 |
This cop checks for unwanted parentheses in parameterless method calls.
Style/MethodCalledOnDoEndBlock
Style/MethodDefParentheses
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.16 |
1.7 |
This cop checks for parentheses around the arguments in method definitions. Both instance and class/singleton methods are checked.
Regardless of style, parentheses are necessary for:
-
Endless methods
-
Argument lists containing a
forward-arg
(…
) -
Argument lists containing an anonymous block forwarding (
&
)
Removing the parens would be a syntax error here.
Examples
EnforcedStyle: require_parentheses (default)
# The `require_parentheses` style requires method definitions
# to always use parentheses
# bad
def bar num1, num2
num1 + num2
end
def foo descriptive_var_name,
another_descriptive_var_name,
last_descriptive_var_name
do_something
end
# good
def bar(num1, num2)
num1 + num2
end
def foo(descriptive_var_name,
another_descriptive_var_name,
last_descriptive_var_name)
do_something
end
EnforcedStyle: require_no_parentheses
# The `require_no_parentheses` style requires method definitions
# to never use parentheses
# bad
def bar(num1, num2)
num1 + num2
end
def foo(descriptive_var_name,
another_descriptive_var_name,
last_descriptive_var_name)
do_something
end
# good
def bar num1, num2
num1 + num2
end
def foo descriptive_var_name,
another_descriptive_var_name,
last_descriptive_var_name
do_something
end
EnforcedStyle: require_no_parentheses_except_multiline
# The `require_no_parentheses_except_multiline` style prefers no
# parentheses when method definition arguments fit on single line,
# but prefers parentheses when arguments span multiple lines.
# bad
def bar(num1, num2)
num1 + num2
end
def foo descriptive_var_name,
another_descriptive_var_name,
last_descriptive_var_name
do_something
end
# good
def bar num1, num2
num1 + num2
end
def foo(descriptive_var_name,
another_descriptive_var_name,
last_descriptive_var_name)
do_something
end
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle |
|
|
Style/MissingElse
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Disabled |
Yes |
No |
0.30 |
0.38 |
Checks for if
expressions that do not have an else
branch.
Pattern matching is allowed to have no else branch because unlike if and case ,
it raises NoMatchingPatternError if the pattern doesn’t match and without having else .
|
Supported styles are: if, case, both.
Examples
EnforcedStyle: if
# warn when an `if` expression is missing an `else` branch.
# bad
if condition
statement
end
# good
if condition
statement
else
# the content of `else` branch will be determined by Style/EmptyElse
end
# good
case var
when condition
statement
end
# good
case var
when condition
statement
else
# the content of `else` branch will be determined by Style/EmptyElse
end
EnforcedStyle: case
# warn when a `case` expression is missing an `else` branch.
# bad
case var
when condition
statement
end
# good
case var
when condition
statement
else
# the content of `else` branch will be determined by Style/EmptyElse
end
# good
if condition
statement
end
# good
if condition
statement
else
# the content of `else` branch will be determined by Style/EmptyElse
end
EnforcedStyle: both (default)
# warn when an `if` or `case` expression is missing an `else` branch.
# bad
if condition
statement
end
# bad
case var
when condition
statement
end
# good
if condition
statement
else
# the content of `else` branch will be determined by Style/EmptyElse
end
# good
case var
when condition
statement
else
# the content of `else` branch will be determined by Style/EmptyElse
end
Style/MissingRespondToMissing
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
No |
0.56 |
- |
This cop checks for the presence of method_missing
without also
defining respond_to_missing?
.
Examples
#bad
def method_missing(name, *args)
# ...
end
#good
def respond_to_missing?(name, include_private)
# ...
end
def method_missing(name, *args)
# ...
end
Style/MixinGrouping
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.48 |
0.49 |
This cop checks for grouping of mixins in class
and module
bodies.
By default it enforces mixins to be placed in separate declarations,
but it can be configured to enforce grouping them in one declaration.
Examples
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle |
|
|
Style/MixinUsage
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
No |
0.51 |
- |
This cop checks that include
, extend
and prepend
statements appear
inside classes and modules, not at the top level, so as to not affect
the behavior of Object
.
Style/ModuleFunction
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes (Unsafe) |
0.11 |
0.65 |
This cop checks for use of extend self
or module_function
in a
module.
Supported styles are: module_function, extend_self, forbidden. forbidden
style prohibits the usage of both styles.
the cop won’t be activated when the module contains any private methods. |
Safety
Autocorrection is unsafe (and is disabled by default) because extend self
and module_function
do not behave exactly the same.
Examples
EnforcedStyle: module_function (default)
# bad
module Test
extend self
# ...
end
# good
module Test
module_function
# ...
end
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle |
|
|
Autocorrect |
|
Boolean |
Style/MultilineBlockChain
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
No |
0.13 |
- |
This cop checks for chaining of a block after another block that spans multiple lines.
Examples
# bad
Thread.list.select do |t|
t.alive?
end.map do |t|
t.object_id
end
# good
alive_threads = Thread.list.select do |t|
t.alive?
end
alive_threads.map do |t|
t.object_id
end
Style/MultilineIfModifier
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.45 |
- |
Checks for uses of if/unless modifiers with multiple-lines bodies.
Style/MultilineIfThen
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.9 |
0.26 |
Checks for uses of the then
keyword in multi-line if statements.
Examples
# bad
# This is considered bad practice.
if cond then
end
# good
# If statements can contain `then` on the same line.
if cond then a
elsif cond then b
end
Style/MultilineInPatternThen
Required Ruby version: 2.7 |
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Pending |
Yes |
Yes |
1.16 |
- |
This cop checks uses of the then
keyword in multi-line in
statement.
Examples
# bad
case expression
in pattern then
end
# good
case expression
in pattern
end
# good
case expression
in pattern then do_something
end
# good
case expression
in pattern then do_something(arg1,
arg2)
end
Style/MultilineMemoization
Style/MultilineTernaryOperator
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.9 |
0.86 |
This cop checks for multi-line ternary op expressions.
return if … else … end is syntax error. If return is used before
multiline ternary operator expression, it cannot be auto-corrected.
|
Examples
# bad
a = cond ?
b : c
a = cond ? b :
c
a = cond ?
b :
c
# good
a = cond ? b : c
a = if cond
b
else
c
end
Style/MultilineWhenThen
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.73 |
- |
This cop checks uses of the then
keyword
in multi-line when statements.
Examples
# bad
case foo
when bar then
end
# good
case foo
when bar
end
# good
case foo
when bar then do_something
end
# good
case foo
when bar then do_something(arg1,
arg2)
end
Style/MultipleComparison
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
1.1 |
This cop checks against comparing a variable with multiple items, where
Array#include?
, Set#include?
or a case
could be used instead
to avoid code repetition.
It accepts comparisons of multiple method calls to avoid unnecessary method calls
by default. It can be configured by AllowMethodComparison
option.
Examples
# bad
a = 'a'
foo if a == 'a' || a == 'b' || a == 'c'
# good
a = 'a'
foo if ['a', 'b', 'c'].include?(a)
VALUES = Set['a', 'b', 'c'].freeze
# elsewhere...
foo if VALUES.include?(a)
case foo
when 'a', 'b', 'c' then foo
# ...
end
# accepted (but consider `case` as above)
foo if a == b.lightweight || a == b.heavyweight
Style/MutableConstant
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes (Unsafe) |
0.34 |
1.8 |
This cop checks whether some constant value isn’t a mutable literal (e.g. array or hash).
Strict mode can be used to freeze all constants, rather than just literals. Strict mode is considered an experimental feature. It has not been updated with an exhaustive list of all methods that will produce frozen objects so there is a decent chance of getting some false positives. Luckily, there is no harm in freezing an already frozen object.
From Ruby 3.0, this cop honours the magic comment 'shareable_constant_value'. When this magic comment is set to any acceptable value other than none, it will suppress the offenses raised by this cop. It enforces frozen state.
Regexp and Range literals are frozen objects since Ruby 3.0. |
From Ruby 3.0, interpolated strings are not frozen when
# frozen-string-literal: true is used, so this cop enforces explicit
freezing for such strings.
|
From Ruby 3.0, this cop allows explicit freezing of constants when
the shareable_constant_value directive is used.
|
Safety
This cop’s autocorrection is unsafe since any mutations on objects that
are made frozen will change from being accepted to raising FrozenError
,
and will need to be manually refactored.
Examples
EnforcedStyle: literals (default)
# bad
CONST = [1, 2, 3]
# good
CONST = [1, 2, 3].freeze
# good
CONST = <<~TESTING.freeze
This is a heredoc
TESTING
# good
CONST = Something.new
EnforcedStyle: strict
# bad
CONST = Something.new
# bad
CONST = Struct.new do
def foo
puts 1
end
end
# good
CONST = Something.new.freeze
# good
CONST = Struct.new do
def foo
puts 1
end
end.freeze
# Magic comment - shareable_constant_value: literal
# bad
CONST = [1, 2, 3]
# good
# shareable_constant_value: literal
CONST = [1, 2, 3]
Style/NegatedIf
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.20 |
0.48 |
Checks for uses of if with a negated condition. Only ifs without else are considered. There are three different styles:
-
both
-
prefix
-
postfix
Examples
EnforcedStyle: both (default)
# enforces `unless` for `prefix` and `postfix` conditionals
# bad
if !foo
bar
end
# good
unless foo
bar
end
# bad
bar if !foo
# good
bar unless foo
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle |
|
|
Style/NegatedIfElseCondition
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Pending |
Yes |
Yes |
1.2 |
- |
This cop checks for uses of if-else
and ternary operators with a negated condition
which can be simplified by inverting condition and swapping branches.
Style/NegatedUnless
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.69 |
- |
Checks for uses of unless with a negated condition. Only unless without else are considered. There are three different styles:
-
both
-
prefix
-
postfix
Examples
EnforcedStyle: both (default)
# enforces `if` for `prefix` and `postfix` conditionals
# bad
unless !foo
bar
end
# good
if foo
bar
end
# bad
bar unless !foo
# good
bar if foo
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle |
|
|
Style/NegatedWhile
Style/NestedModifier
Style/NestedParenthesizedCalls
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.36 |
0.77 |
This cop checks for unparenthesized method calls in the argument list of a parenthesized method call.
Style/NestedTernaryOperator
Style/Next
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.22 |
0.35 |
Use next
to skip iteration instead of a condition at the end.
Examples
EnforcedStyle: skip_modifier_ifs (default)
# bad
[1, 2].each do |a|
if a == 1
puts a
end
end
# good
[1, 2].each do |a|
next unless a == 1
puts a
end
# good
[1, 2].each do |a|
puts a if a == 1
end
EnforcedStyle: always
# With `always` all conditions at the end of an iteration needs to be
# replaced by next - with `skip_modifier_ifs` the modifier if like
# this one are ignored: `[1, 2].each { |a| puts a if a == 1 }`
# bad
[1, 2].each do |a|
puts a if a == 1
end
# bad
[1, 2].each do |a|
if a == 1
puts a
end
end
# good
[1, 2].each do |a|
next unless a == 1
puts a
end
Style/NilComparison
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.12 |
0.59 |
This cop checks for comparison of something with nil using ==
and
nil?
.
Supported styles are: predicate, comparison.
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle |
|
|
Style/NilLambda
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Pending |
Yes |
Yes |
1.3 |
1.15 |
This cop checks for lambdas and procs that always return nil, which can be replaced with an empty lambda or proc instead.
Style/NonNilCheck
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.20 |
0.22 |
This cop checks for non-nil checks, which are usually redundant.
With IncludeSemanticChanges
set to false
by default, this cop
does not report offenses for !x.nil?
and does no changes that might
change behavior.
Also IncludeSemanticChanges
set to false
with EnforcedStyle: comparison
of
Style/NilComparison
cop, this cop does not report offenses for x != nil
and
does no changes to !x.nil?
style.
With IncludeSemanticChanges
set to true
, this cop reports offenses
for !x.nil?
and autocorrects that and x != nil
to solely x
, which
is usually OK, but might change behavior.
Examples
# bad
if x != nil
end
# good
if x
end
# Non-nil checks are allowed if they are the final nodes of predicate.
# good
def signed_in?
!current_user.nil?
end
Style/Not
Style/NumberedParameters
Required Ruby version: 2.7 |
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Pending |
Yes |
No |
1.22 |
- |
This cop checks for numbered parameters.
It can either restrict the use of numbered parameters to single-lined blocks, or disallow completely numbered parameters.
Style/NumberedParametersLimit
Required Ruby version: 2.7 |
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Pending |
Yes |
No |
1.22 |
- |
This cop detects use of an excessive amount of numbered parameters in a single block. Having too many numbered parameters can make code too cryptic and hard to read.
The cop defaults to registering an offense if there is more than 1 numbered
parameter but this maximum can be configured by setting Max
.
Style/NumericLiteralPrefix
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.41 |
- |
This cop checks for octal, hex, binary, and decimal literals using uppercase prefixes and corrects them to lowercase prefix or no prefix (in case of decimals).
Examples
Style/NumericLiterals
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.9 |
0.48 |
This cop checks for big numeric literals without _ between groups of digits in them.
Style/NumericPredicate
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
No |
Yes (Unsafe) |
0.42 |
0.59 |
This cop checks for usage of comparison operators (==
,
>
, <
) to test numbers as zero, positive, or negative.
These can be replaced by their respective predicate methods.
The cop can also be configured to do the reverse.
The cop disregards #nonzero?
as its value is truthy or falsey,
but not true
and false
, and thus not always interchangeable with
!= 0
.
The cop ignores comparisons to global variables, since they are often
populated with objects which can be compared with integers, but are
not themselves Integer
polymorphic.
Safety
This cop is unsafe because it cannot be guaranteed that the receiver defines the predicates or can be compared to a number, which may lead to a false positive for non-standard classes.
Examples
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle |
|
|
IgnoredMethods |
|
Array |
Exclude |
|
Array |
Style/OneLineConditional
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.9 |
0.90 |
Checks for uses of if/then/else/end constructs on a single line. AlwaysCorrectToMultiline config option can be set to true to auto-convert all offenses to multi-line constructs. When AlwaysCorrectToMultiline is false (default case) the auto-correct will first try converting them to ternary operators.
Examples
# bad
if foo then bar else baz end
# bad
unless foo then baz else bar end
# good
foo ? bar : baz
# good
bar if foo
# good
if foo then bar end
# good
if foo
bar
else
baz
end
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
AlwaysCorrectToMultiline |
|
Boolean |
Style/OpenStructUse
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Pending |
Yes |
No |
1.23 |
- |
This cop flags uses of OpenStruct, as it is now officially discouraged to be used for performance, version compatibility, and potential security issues.
Safety
Note that this cop may flag false positives; for instance, the following legal
use of a hand-rolled OpenStruct
type would be considered an offense:
module MyNamespace
class OpenStruct # not the OpenStruct we're looking for
end
def new_struct
OpenStruct.new # resolves to MyNamespace::OpenStruct
end
end
Style/OptionHash
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Disabled |
Yes |
No |
0.33 |
0.34 |
This cop checks for options hashes and discourages them if the current Ruby version supports keyword arguments.
Style/OptionalArguments
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
No |
No |
0.33 |
0.83 |
This cop checks for optional arguments to methods that do not come at the end of the argument list.
Examples
# bad
def foo(a = 1, b, c)
end
# good
def baz(a, b, c = 1)
end
def foobar(a = 1, b = 2, c = 3)
end
Style/OptionalBooleanParameter
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
No |
No |
0.89 |
- |
This cop checks for places where keyword arguments can be used instead of
boolean arguments when defining methods. respond_to_missing?
method is allowed by default.
These are customizable with AllowedMethods
option.
Examples
# bad
def some_method(bar = false)
puts bar
end
# bad - common hack before keyword args were introduced
def some_method(options = {})
bar = options.fetch(:bar, false)
puts bar
end
# good
def some_method(bar: false)
puts bar
end
Style/OrAssignment
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.50 |
- |
This cop checks for potential usage of the ||=
operator.
Style/ParallelAssignment
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.32 |
- |
Checks for simple usages of parallel assignment. This will only complain when the number of variables being assigned matched the number of assigning variables.
Examples
# bad
a, b, c = 1, 2, 3
a, b, c = [1, 2, 3]
# good
one, two = *foo
a, b = foo()
a, b = b, a
a = 1
b = 2
c = 3
Style/ParenthesesAroundCondition
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.9 |
0.56 |
This cop checks for the presence of superfluous parentheses around the condition of if/unless/while/until.
AllowSafeAssignment
option for safe assignment.
By safe assignment we mean putting parentheses around
an assignment to indicate "I know I’m using an assignment
as a condition. It’s not a mistake."
Examples
# bad
x += 1 while (x < 10)
foo unless (bar || baz)
if (x > 10)
elsif (x < 3)
end
# good
x += 1 while x < 10
foo unless bar || baz
if x > 10
elsif x < 3
end
Style/PercentLiteralDelimiters
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.19 |
0.48 |
This cop enforces the consistent usage of %
-literal delimiters.
Specify the 'default' key to set all preferred delimiters at once. You can continue to specify individual preferred delimiters to override the default.
Examples
# Style/PercentLiteralDelimiters:
# PreferredDelimiters:
# default: '[]'
# '%i': '()'
# good
%w[alpha beta] + %i(gamma delta)
# bad
%W(alpha #{beta})
# bad
%I(alpha beta)
Style/PercentQLiterals
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.25 |
- |
This cop checks for usage of the %Q() syntax when %q() would do.
Style/PerlBackrefs
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.13 |
- |
This cop looks for uses of Perl-style regexp match backreferences and their English versions like $1, $2, $&, &+, $MATCH, $PREMATCH, etc.
Style/PreferredHashMethods
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
No |
Yes (Unsafe) |
0.41 |
0.70 |
This cop checks for uses of methods Hash#has_key?
and
Hash#has_value?
, and suggests using Hash#key?
and Hash#value?
instead.
It is configurable to enforce the verbose method names, by using the
EnforcedStyle: verbose
configuration.
Safety
This cop is unsafe because it cannot be guaranteed that the receiver
is a Hash
or responds to the replacement methods.
Style/Proc
Style/QuotedSymbols
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Pending |
Yes |
Yes |
1.16 |
- |
Checks if the quotes used for quoted symbols match the configured defaults.
By default uses the same configuration as Style/StringLiterals
; if that
cop is not enabled, the default EnforcedStyle
is single_quotes
.
String interpolation is always kept in double quotes.
Note: Lint/SymbolConversion
can be used in parallel to ensure that symbols
are not quoted that don’t need to be. This cop is for configuring the quoting
style to use for symbols that require quotes.
Style/RaiseArgs
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.14 |
1.2 |
This cop checks the args passed to fail
and raise
. For exploded
style (default), it recommends passing the exception class and message
to raise
, rather than construct an instance of the error. It will
still allow passing just a message, or the construction of an error
with more than one argument.
The exploded style works identically, but with the addition that it will also suggest constructing error objects when the exception is passed multiple arguments.
The exploded style has an AllowedCompactTypes
configuration
option that takes an Array of exception name Strings.
Examples
EnforcedStyle: exploded (default)
# bad
raise StandardError.new('message')
# good
raise StandardError, 'message'
fail 'message'
raise MyCustomError
raise MyCustomError.new(arg1, arg2, arg3)
raise MyKwArgError.new(key1: val1, key2: val2)
# With `AllowedCompactTypes` set to ['MyWrappedError']
raise MyWrappedError.new(obj)
raise MyWrappedError.new(obj), 'message'
Style/RandomWithOffset
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.52 |
- |
This cop checks for the use of randomly generated numbers, added/subtracted with integer literals, as well as those with Integer#succ and Integer#pred methods. Prefer using ranges instead, as it clearly states the intentions.
Examples
# bad
rand(6) + 1
1 + rand(6)
rand(6) - 1
1 - rand(6)
rand(6).succ
rand(6).pred
Random.rand(6) + 1
Kernel.rand(6) + 1
rand(0..5) + 1
# good
rand(1..6)
rand(1...7)
Style/RedundantArgument
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Pending |
No |
Yes (Unsafe) |
1.4 |
1.7 |
This cop checks for a redundant argument passed to certain methods.
This cop is limited to methods with single parameter. |
Method names and their redundant arguments can be configured like this:
Methods:
join: ''
split: ' '
chomp: "\n"
chomp!: "\n"
foo: 2
Safety
This cop is unsafe because of the following limitations:
-
This cop matches by method names only and hence cannot tell apart methods with same name in different classes.
-
This cop may be unsafe if certain special global variables (e.g.
$;
,$/
) are set. That depends on the nature of the target methods, of course. For example, the default argument to join is$OUTPUT_FIELD_SEPARATOR
(or$,
) rather than''
, and if that global is changed,''
is no longer a redundant argument.
Style/RedundantAssignment
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.87 |
- |
This cop checks for redundant assignment before returning.
Style/RedundantBegin
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.10 |
0.21 |
This cop checks for redundant begin
blocks.
Currently it checks for code like this:
Examples
# bad
def redundant
begin
ala
bala
rescue StandardError => e
something
end
end
# good
def preferred
ala
bala
rescue StandardError => e
something
end
# bad
begin
do_something
end
# good
do_something
# bad
do_something do
begin
something
rescue => ex
anything
end
end
# good
# In Ruby 2.5 or later, you can omit `begin` in `do-end` block.
do_something do
something
rescue => ex
anything
end
# good
# Stabby lambdas don't support implicit `begin` in `do-end` blocks.
-> do
begin
foo
rescue Bar
baz
end
end
Style/RedundantConditional
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.50 |
- |
This cop checks for redundant returning of true/false in conditionals.
Style/RedundantException
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.14 |
0.29 |
This cop checks for RuntimeError as the argument of raise/fail.
It checks for code like this:
Style/RedundantFetchBlock
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
No |
Yes (Unsafe) |
0.86 |
- |
This cop identifies places where fetch(key) { value }
can be replaced by fetch(key, value)
.
In such cases fetch(key, value)
method is faster
than fetch(key) { value }
.
Safety
This cop is unsafe because it cannot be guaranteed that the receiver
does not have a different implementation of fetch
.
Style/RedundantFileExtensionInRequire
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.88 |
- |
This cop checks for the presence of superfluous .rb
extension in
the filename provided to require
and require_relative
.
Note: If the extension is omitted, Ruby tries adding '.rb', '.so',
and so on to the name until found. If the file named cannot be found,
a LoadError
will be raised.
There is an edge case where foo.so
file is loaded instead of a LoadError
if foo.so
file exists when require 'foo.rb'
will be changed to require 'foo'
,
but that seems harmless.
Style/RedundantFreeze
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.34 |
0.66 |
This cop check for uses of Object#freeze
on immutable objects.
Regexp and Range literals are frozen objects since Ruby 3.0. |
From Ruby 3.0, this cop allows explicit freezing of interpolated
string literals when # frozen-string-literal: true is used.
|
Style/RedundantPercentQ
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.76 |
- |
This cop checks for usage of the %q/%Q syntax when '' or "" would do.
Examples
# bad
name = %q(Bruce Wayne)
time = %q(8 o'clock)
question = %q("What did you say?")
# good
name = 'Bruce Wayne'
time = "8 o'clock"
question = '"What did you say?"'
Style/RedundantRegexpCharacterClass
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.85 |
- |
This cop checks for unnecessary single-element Regexp character classes.
Style/RedundantRegexpEscape
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.85 |
- |
This cop checks for redundant escapes inside Regexp literals.
Style/RedundantReturn
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.10 |
0.14 |
This cop checks for redundant return
expressions.
Examples
# These bad cases should be extended to handle methods whose body is
# if/else or a case expression with a default branch.
# bad
def test
return something
end
# bad
def test
one
two
three
return something
end
# good
def test
return something if something_else
end
# good
def test
if x
elsif y
else
end
end
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
AllowMultipleReturnValues |
|
Boolean |
Style/RedundantSelf
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.10 |
0.13 |
This cop checks for redundant uses of self
.
The usage of self
is only needed when:
-
Sending a message to same object with zero arguments in presence of a method name clash with an argument or a local variable.
-
Calling an attribute writer to prevent a local variable assignment.
Note, with using explicit self you can only send messages with public or protected scope, you cannot send private messages this way.
Note we allow uses of self
with operators because it would be awkward
otherwise.
Style/RedundantSelfAssignment
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
No |
Yes (Unsafe) |
0.90 |
- |
This cop checks for places where redundant assignments are made for in place modification methods.
Style/RedundantSelfAssignmentBranch
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Pending |
Yes |
Yes |
1.19 |
- |
This cop checks for places where conditional branch makes redundant self-assignment.
It only detects local variable because it may replace state of instance variable,
class variable, and global variable that have state across methods with nil
.
Style/RedundantSort
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
No |
Yes (Unsafe) |
0.76 |
1.22 |
This cop is used to identify instances of sorting and then
taking only the first or last element. The same behavior can
be accomplished without a relatively expensive sort by using
Enumerable#min
instead of sorting and taking the first
element and Enumerable#max
instead of sorting and taking the
last element. Similarly, Enumerable#min_by
and
Enumerable#max_by
can replace Enumerable#sort_by
calls
after which only the first or last element is used.
Safety
This cop is unsafe, because sort…last
and max
may not return the
same element in all cases.
In an enumerable where there are multiple elements where a <⇒ b == 0
,
or where the transformation done by the sort_by
block has the
same result, sort.last
and max
(or sort_by.last
and max_by
)
will return different elements. sort.last
will return the last
element but max
will return the first element.
For example:
class MyString < String; end
strings = [MyString.new('test'), 'test']
strings.sort.last.class #=> String
strings.max.class #=> MyString
words = %w(dog horse mouse)
words.sort_by { |word| word.length }.last #=> 'mouse'
words.max_by { |word| word.length } #=> 'horse'
Examples
# bad
[2, 1, 3].sort.first
[2, 1, 3].sort[0]
[2, 1, 3].sort.at(0)
[2, 1, 3].sort.slice(0)
# good
[2, 1, 3].min
# bad
[2, 1, 3].sort.last
[2, 1, 3].sort[-1]
[2, 1, 3].sort.at(-1)
[2, 1, 3].sort.slice(-1)
# good
[2, 1, 3].max
# bad
arr.sort_by(&:foo).first
arr.sort_by(&:foo)[0]
arr.sort_by(&:foo).at(0)
arr.sort_by(&:foo).slice(0)
# good
arr.min_by(&:foo)
# bad
arr.sort_by(&:foo).last
arr.sort_by(&:foo)[-1]
arr.sort_by(&:foo).at(-1)
arr.sort_by(&:foo).slice(-1)
# good
arr.max_by(&:foo)
Style/RegexpLiteral
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.9 |
0.30 |
This cop enforces using // or %r around regular expressions.
Examples
EnforcedStyle: slashes (default)
# bad
snake_case = %r{^[\dA-Z_]+$}
# bad
regex = %r{
foo
(bar)
(baz)
}x
# good
snake_case = /^[\dA-Z_]+$/
# good
regex = /
foo
(bar)
(baz)
/x
EnforcedStyle: percent_r
# bad
snake_case = /^[\dA-Z_]+$/
# bad
regex = /
foo
(bar)
(baz)
/x
# good
snake_case = %r{^[\dA-Z_]+$}
# good
regex = %r{
foo
(bar)
(baz)
}x
EnforcedStyle: mixed
# bad
snake_case = %r{^[\dA-Z_]+$}
# bad
regex = /
foo
(bar)
(baz)
/x
# good
snake_case = /^[\dA-Z_]+$/
# good
regex = %r{
foo
(bar)
(baz)
}x
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle |
|
|
AllowInnerSlashes |
|
Boolean |
Style/RescueModifier
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.9 |
0.34 |
This cop checks for uses of rescue in its modifier form.
The cop to check rescue
in its modifier form is added for following
reasons:
-
The syntax of modifier form
rescue
can be misleading because it might led us to believe thatrescue
handles the given exception but it actually rescue all exceptions to return the given rescue block. In this case, value returned by handle_error or SomeException. -
Modifier form
rescue
would rescue all the exceptions. It would silently skip all exception or errors and handle the error. Example: IfNoMethodError
is raised, modifier form rescue would handle the exception.
Examples
# bad
some_method rescue handle_error
# bad
some_method rescue SomeException
# good
begin
some_method
rescue
handle_error
end
# good
begin
some_method
rescue SomeException
handle_error
end
Style/RescueStandardError
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.52 |
- |
This cop checks for rescuing StandardError
. There are two supported
styles implicit
and explicit
. This cop will not register an offense
if any error other than StandardError
is specified.
Style/ReturnNil
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Disabled |
Yes |
Yes |
0.50 |
- |
This cop enforces consistency between 'return nil' and 'return'.
Supported styles are: return, return_nil.
Style/SafeNavigation
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes (Unsafe) |
0.43 |
0.77 |
This cop transforms usages of a method call safeguarded by a non nil
check for the variable whose method is being called to
safe navigation (&.
). If there is a method chain, all of the methods
in the chain need to be checked for safety, and all of the methods will
need to be changed to use safe navigation. We have limited the cop to
not register an offense for method chains that exceed 2 methods.
The default for ConvertCodeThatCanStartToReturnNil
is false
.
When configured to true
, this will
check for code in the format !foo.nil? && foo.bar
. As it is written,
the return of this code is limited to false
and whatever the return
of the method is. If this is converted to safe navigation,
foo&.bar
can start returning nil
as well as what the method
returns.
Safety
Autocorrection is unsafe because if a value is false
, the resulting
code will have different behaviour or raise an error.
x = false
x && x.foo # return false
x&.foo # raises NoMethodError
Examples
# bad
foo.bar if foo
foo.bar.baz if foo
foo.bar(param1, param2) if foo
foo.bar { |e| e.something } if foo
foo.bar(param) { |e| e.something } if foo
foo.bar if !foo.nil?
foo.bar unless !foo
foo.bar unless foo.nil?
foo && foo.bar
foo && foo.bar.baz
foo && foo.bar(param1, param2)
foo && foo.bar { |e| e.something }
foo && foo.bar(param) { |e| e.something }
# good
foo&.bar
foo&.bar&.baz
foo&.bar(param1, param2)
foo&.bar { |e| e.something }
foo&.bar(param) { |e| e.something }
foo && foo.bar.baz.qux # method chain with more than 2 methods
foo && foo.nil? # method that `nil` responds to
# Method calls that do not use `.`
foo && foo < bar
foo < bar if foo
# When checking `foo&.empty?` in a conditional, `foo` being `nil` will actually
# do the opposite of what the author intends.
foo && foo.empty?
# This could start returning `nil` as well as the return of the method
foo.nil? || foo.bar
!foo || foo.bar
# Methods that are used on assignment, arithmetic operation or
# comparison should not be converted to use safe navigation
foo.baz = bar if foo
foo.baz + bar if foo
foo.bar > 2 if foo
Style/Sample
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.30 |
- |
This cop is used to identify usages of shuffle.first
,
shuffle.last
, and shuffle[]
and change them to use
sample
instead.
Examples
# bad
[1, 2, 3].shuffle.first
[1, 2, 3].shuffle.first(2)
[1, 2, 3].shuffle.last
[2, 1, 3].shuffle.at(0)
[2, 1, 3].shuffle.slice(0)
[1, 2, 3].shuffle[2]
[1, 2, 3].shuffle[0, 2] # sample(2) will do the same
[1, 2, 3].shuffle[0..2] # sample(3) will do the same
[1, 2, 3].shuffle(random: Random.new).first
# good
[1, 2, 3].shuffle
[1, 2, 3].sample
[1, 2, 3].sample(3)
[1, 2, 3].shuffle[1, 3] # sample(3) might return a longer Array
[1, 2, 3].shuffle[1..3] # sample(3) might return a longer Array
[1, 2, 3].shuffle[foo, bar]
[1, 2, 3].shuffle(random: Random.new)
Style/SelectByRegexp
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Pending |
Yes |
Yes (Unsafe) |
1.22 |
- |
This cop looks for places where an subset of an Enumerable (array,
range, set, etc.; see note below) is calculated based on a Regexp
match, and suggests grep
or grep_v
instead.
Hashes do not behave as you may expect with grep , which
means that hash.grep is not equivalent to hash.select . Although
RuboCop is limited by static analysis, this cop attempts to avoid
registering an offense when the receiver is a hash (hash literal,
Hash.new , Hash#[] , or to_h /to_hash ).
|
grep and grep_v were optimized when used without a block
in Ruby 3.0, but may be slower in previous versions.
See https://bugs.ruby-lang.org/issues/17030
|
Safety
Autocorrection is marked as unsafe because MatchData
will
not be created by grep
, but may have previously been relied
upon after the match?
or =~
call.
Additionally, the cop cannot guarantee that the receiver of
select
or reject
is actually an array by static analysis,
so the correction may not be actually equivalent.
Examples
# bad (select or find_all)
array.select { |x| x.match? /regexp/ }
array.select { |x| /regexp/.match?(x) }
array.select { |x| x =~ /regexp/ }
array.select { |x| /regexp/ =~ x }
# bad (reject)
array.reject { |x| x.match? /regexp/ }
array.reject { |x| /regexp/.match?(x) }
array.reject { |x| x =~ /regexp/ }
array.reject { |x| /regexp/ =~ x }
# good
array.grep(regexp)
array.grep_v(regexp)
Style/SelfAssignment
Style/Semicolon
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.9 |
0.19 |
This cop checks for multiple expressions placed on the same line. It also checks for lines terminated with a semicolon.
This cop has AllowAsExpressionSeparator
configuration option.
It allows ;
to separate several expressions on the same line.
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
AllowAsExpressionSeparator |
|
Boolean |
Style/Send
Style/SignalException
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.11 |
0.37 |
This cop checks for uses of fail
and raise
.
Examples
EnforcedStyle: only_raise (default)
# The `only_raise` style enforces the sole use of `raise`.
# bad
begin
fail
rescue Exception
# handle it
end
def watch_out
fail
rescue Exception
# handle it
end
Kernel.fail
# good
begin
raise
rescue Exception
# handle it
end
def watch_out
raise
rescue Exception
# handle it
end
Kernel.raise
EnforcedStyle: only_fail
# The `only_fail` style enforces the sole use of `fail`.
# bad
begin
raise
rescue Exception
# handle it
end
def watch_out
raise
rescue Exception
# handle it
end
Kernel.raise
# good
begin
fail
rescue Exception
# handle it
end
def watch_out
fail
rescue Exception
# handle it
end
Kernel.fail
EnforcedStyle: semantic
# The `semantic` style enforces the use of `fail` to signal an
# exception, then will use `raise` to trigger an offense after
# it has been rescued.
# bad
begin
raise
rescue Exception
# handle it
end
def watch_out
# Error thrown
rescue Exception
fail
end
Kernel.fail
Kernel.raise
# good
begin
fail
rescue Exception
# handle it
end
def watch_out
fail
rescue Exception
raise 'Preferably with descriptive message'
end
explicit_receiver.fail
explicit_receiver.raise
Style/SingleArgumentDig
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
No |
Yes (Unsafe) |
0.89 |
- |
Sometimes using dig method ends up with just a single argument. In such cases, dig should be replaced with [].
Style/SingleLineBlockParams
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Disabled |
Yes |
Yes |
0.16 |
1.6 |
This cop checks whether the block parameters of a single-line method accepting a block match the names specified via configuration.
For instance one can configure reduce
(inject
) to use |a, e| as
parameters.
Configuration option: Methods Should be set to use this cop. Array of hashes, where each key is the method name and value - array of argument names.
Style/SingleLineMethods
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.9 |
1.8 |
This cop checks for single-line method definitions that contain a body. It will accept single-line methods with no body.
Endless methods added in Ruby 3.0 are also accepted by this cop.
If Style/EndlessMethod
is enabled with EnforcedStyle: allow_single_line
or
allow_always
, single-line methods will be auto-corrected to endless
methods if there is only one statement in the body.
Style/SlicingWithRange
Required Ruby version: 2.6 |
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
No |
Yes (Unsafe) |
0.83 |
- |
This cop checks that arrays are sliced with endless ranges instead of
ary[start..-1]
on Ruby 2.6+.
Style/SoleNestedConditional
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.89 |
1.5 |
If the branch of a conditional consists solely of a conditional node, its conditions can be combined with the conditions of the outer branch. This helps to keep the nesting level from getting too deep.
Style/SpecialGlobalVars
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes (Unsafe) |
0.13 |
0.36 |
This cop looks for uses of Perl-style global variables. Correcting to global variables in the 'English' library will add a require statement to the top of the file if enabled by RequireEnglish config.
Safety
Autocorrection is marked as unsafe because if RequireEnglish
is not
true, replacing perl-style variables with english variables will break.
Examples
EnforcedStyle: use_english_names (default)
# good
require 'English' # or this could be in another file.
puts $LOAD_PATH
puts $LOADED_FEATURES
puts $PROGRAM_NAME
puts $ERROR_INFO
puts $ERROR_POSITION
puts $FIELD_SEPARATOR # or $FS
puts $OUTPUT_FIELD_SEPARATOR # or $OFS
puts $INPUT_RECORD_SEPARATOR # or $RS
puts $OUTPUT_RECORD_SEPARATOR # or $ORS
puts $INPUT_LINE_NUMBER # or $NR
puts $LAST_READ_LINE
puts $DEFAULT_OUTPUT
puts $DEFAULT_INPUT
puts $PROCESS_ID # or $PID
puts $CHILD_STATUS
puts $LAST_MATCH_INFO
puts $IGNORECASE
puts $ARGV # or ARGV
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
RequireEnglish |
|
Boolean |
EnforcedStyle |
|
|
Style/StabbyLambdaParentheses
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.35 |
- |
Check for parentheses around stabby lambda arguments.
There are two different styles. Defaults to require_parentheses
.
Examples
Style/StaticClass
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Disabled |
No |
No |
1.3 |
- |
This cop checks for places where classes with only class methods can be replaced with a module. Classes should be used only when it makes sense to create instances out of them.
Safety
This cop is unsafe, because it is possible that this class is a parent for some other subclass, monkey-patched with instance methods or a dummy instance is instantiated from it somewhere.
Examples
# bad
class SomeClass
def self.some_method
# body omitted
end
def self.some_other_method
# body omitted
end
end
# good
module SomeModule
module_function
def some_method
# body omitted
end
def some_other_method
# body omitted
end
end
# good - has instance method
class SomeClass
def instance_method; end
def self.class_method; end
end
Style/StderrPuts
Style/StringChars
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Pending |
No |
Yes (Unsafe) |
1.12 |
- |
Checks for uses of String#split
with empty string or regexp literal argument.
Safety
This cop is unsafe because it cannot be guaranteed that the receiver
is actually a string. If another class has a split
method with
different behaviour, it would be registered as a false positive.
Style/StringConcatenation
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
No |
Yes (Unsafe) |
0.89 |
1.18 |
This cop checks for places where string concatenation can be replaced with string interpolation.
The cop can autocorrect simple cases but will skip autocorrecting more complex cases where the resulting code would be harder to read. In those cases, it might be useful to extract statements to local variables or methods which you can then interpolate in a string.
When concatenation between two strings is broken over multiple
lines, this cop does not register an offense; instead,
Style/LineEndConcatenation will pick up the offense if enabled.
|
Two modes are supported:
1. aggressive
style checks and corrects all occurrences of +
where
either the left or right side of +
is a string literal.
2. conservative
style on the other hand, checks and corrects only if
left side (receiver of +
method call) is a string literal.
This is useful when the receiver is some expression that returns string like Pathname
instead of a string literal.
Safety
This cop is unsafe in aggressive
mode, as it cannot be guaranteed that
the receiver is actually a string, which can result in a false positive.
Examples
Mode: aggressive (default)
# bad
email_with_name = user.name + ' <' + user.email + '>'
Pathname.new('/') + 'test'
# good
email_with_name = "#{user.name} <#{user.email}>"
email_with_name = format('%s <%s>', user.name, user.email)
"#{Pathname.new('/')}test"
# accepted, line-end concatenation
name = 'First' +
'Last'
Style/StringHashKeys
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Disabled |
No |
Yes (Unsafe) |
0.52 |
0.75 |
This cop checks for the use of strings as keys in hashes. The use of symbols is preferred instead.
Safety
This cop is unsafe because while symbols are preferred for hash keys, there are instances when string keys are required.
Style/StringLiterals
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.9 |
0.36 |
Checks if uses of quotes match the configured preference.
Examples
Style/StringLiteralsInInterpolation
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.27 |
- |
This cop checks that quotes inside the string interpolation match the configured preference.
Style/StringMethods
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Disabled |
Yes |
Yes |
0.34 |
0.34 |
This cop enforces the use of consistent method names from the String class.
Style/StructInheritance
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes (Unsafe) |
0.29 |
1.20 |
This cop checks for inheritance from Struct.new.
Safety
Auto-correction is unsafe because it will change the inheritance
tree (e.g. return value of Module#ancestors
) of the constant.
Examples
# bad
class Person < Struct.new(:first_name, :last_name)
def age
42
end
end
# good
Person = Struct.new(:first_name, :last_name) do
def age
42
end
end
Style/SwapValues
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Pending |
Yes |
Yes (Unsafe) |
1.1 |
- |
This cop enforces the use of shorthand-style swapping of 2 variables.
Safety
Autocorrection is unsafe, because the temporary variable used to swap variables will be removed, but may be referred to elsewhere.
Style/SymbolArray
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.9 |
0.49 |
This cop can check for array literals made up of symbols that are not using the %i() syntax.
Alternatively, it checks for symbol arrays using the %i() syntax on projects which do not want to use that syntax.
Configuration option: MinSize
If set, arrays with fewer elements than this value will not trigger the
cop. For example, a MinSize
of 3
will not enforce a style on an
array of 2 or fewer elements.
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle |
|
|
MinSize |
|
Integer |
Style/SymbolProc
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
No |
Yes (Unsafe) |
0.26 |
1.5 |
Use symbols as procs when possible.
If you prefer a style that allows block for method with arguments,
please set true
to AllowMethodsWithArguments
.
Safety
This cop is unsafe because proc`s and blocks work differently
when additional arguments are passed in. A block will silently
ignore additional arguments, but a `proc
will raise
an ArgumentError
.
For example:
class Foo
def bar
:bar
end
end
def call(options = {}, &block)
block.call(Foo.new, options)
end
call { |x| x.bar }
#=> :bar
call(&:bar)
# ArgumentError: wrong number of arguments (given 1, expected 0)
Style/TernaryParentheses
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.42 |
0.46 |
This cop checks for the presence of parentheses around ternary
conditions. It is configurable to enforce inclusion or omission of
parentheses using EnforcedStyle
. Omission is only enforced when
removing the parentheses won’t cause a different behavior.
AllowSafeAssignment
option for safe assignment.
By safe assignment we mean putting parentheses around
an assignment to indicate "I know I’m using an assignment
as a condition. It’s not a mistake."
Examples
EnforcedStyle: require_no_parentheses (default)
# bad
foo = (bar?) ? a : b
foo = (bar.baz?) ? a : b
foo = (bar && baz) ? a : b
# good
foo = bar? ? a : b
foo = bar.baz? ? a : b
foo = bar && baz ? a : b
EnforcedStyle: require_parentheses
# bad
foo = bar? ? a : b
foo = bar.baz? ? a : b
foo = bar && baz ? a : b
# good
foo = (bar?) ? a : b
foo = (bar.baz?) ? a : b
foo = (bar && baz) ? a : b
Style/TopLevelMethodDefinition
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Disabled |
Yes |
No |
1.15 |
- |
Newcomers to ruby applications may write top-level methods, when ideally they should be organized in appropriate classes or modules. This cop looks for definitions of top-level methods and warns about them.
However for ruby scripts it is perfectly fine to use top-level methods. Hence this cop is disabled by default.
Examples
# bad
def some_method
end
# bad
def self.some_method
end
# bad
define_method(:foo) { puts 1 }
# good
module Foo
def some_method
end
end
# good
class Foo
def self.some_method
end
end
# good
Struct.new do
def some_method
end
end
# good
class Foo
define_method(:foo) { puts 1 }
end
Style/TrailingBodyOnMethodDefinition
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.52 |
- |
This cop checks for trailing code after the method definition.
It always accepts endless method definitions that are basically on the same line. |
Style/TrailingCommaInArguments
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.36 |
- |
This cop checks for trailing comma in argument lists. The supported styles are:
-
consistent_comma
: Requires a comma after the last argument, for all parenthesized method calls with arguments. -
comma
: Requires a comma after the last argument, but only for parenthesized method calls where each argument is on its own line. -
no_comma
: Requires that there is no comma after the last argument.
Examples
EnforcedStyleForMultiline: consistent_comma
# bad
method(1, 2,)
# good
method(1, 2)
# good
method(
1, 2,
3,
)
# good
method(
1, 2, 3,
)
# good
method(
1,
2,
)
Style/TrailingCommaInArrayLiteral
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.53 |
- |
This cop checks for trailing comma in array literals. The configuration options are:
-
consistent_comma
: Requires a comma after the last item of all non-empty, multiline array literals. -
comma
: Requires a comma after last item in an array, but only when each item is on its own line. -
no_comma
: Does not requires a comma after the last item in an array
Examples
EnforcedStyleForMultiline: consistent_comma
# bad
a = [1, 2,]
# good
a = [1, 2]
# good
a = [
1, 2,
3,
]
# good
a = [
1, 2, 3,
]
# good
a = [
1,
2,
]
Style/TrailingCommaInBlockArgs
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Disabled |
No |
Yes (Unsafe) |
0.81 |
- |
This cop checks whether trailing commas in block arguments are required. Blocks with only one argument and a trailing comma require that comma to be present. Blocks with more than one argument never require a trailing comma.
Safety
This cop is unsafe because a trailing comma can indicate there are more parameters that are not used.
For example:
# with a trailing comma
{foo: 1, bar: 2, baz: 3}.map {|key,| key }
#=> [:foo, :bar, :baz]
# without a trailing comma
{foo: 1, bar: 2, baz: 3}.map {|key| key }
#=> [[:foo, 1], [:bar, 2], [:baz, 3]]
This can be fixed by replacing the trailing comma with a placeholder
argument (such as |key, _value|
).
Style/TrailingCommaInHashLiteral
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.53 |
- |
This cop checks for trailing comma in hash literals. The configuration options are:
-
consistent_comma
: Requires a comma after the last item of all non-empty, multiline hash literals. -
comma
: Requires a comma after the last item in a hash, but only when each item is on its own line. -
no_comma
: Does not requires a comma after the last item in a hash
Examples
EnforcedStyleForMultiline: consistent_comma
# bad
a = { foo: 1, bar: 2, }
# good
a = { foo: 1, bar: 2 }
# good
a = {
foo: 1, bar: 2,
qux: 3,
}
# good
a = {
foo: 1, bar: 2, qux: 3,
}
# good
a = {
foo: 1,
bar: 2,
}
Style/TrailingMethodEndStatement
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.52 |
- |
This cop checks for trailing code after the method definition.
Style/TrailingUnderscoreVariable
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.31 |
0.35 |
This cop checks for extra underscores in variable assignment.
Style/TrivialAccessors
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.9 |
1.15 |
This cop looks for trivial reader/writer methods, that could have been created with the attr_* family of functions automatically.
Examples
# bad
def foo
@foo
end
def bar=(val)
@bar = val
end
def self.baz
@baz
end
# good
attr_reader :foo
attr_writer :bar
class << self
attr_reader :baz
end
AllowDSLWriters: false
# bad
def on_exception(action)
@on_exception=action
end
# good
attr_writer :on_exception
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
ExactNameMatch |
|
Boolean |
AllowPredicates |
|
Boolean |
AllowDSLWriters |
|
Boolean |
IgnoreClassMethods |
|
Boolean |
AllowedMethods |
|
Array |
Style/UnlessElse
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.9 |
- |
This cop looks for unless
expressions with else
clauses.
Examples
# bad
unless foo_bar.nil?
# do something...
else
# do a different thing...
end
# good
if foo_bar.present?
# do something...
else
# do a different thing...
end
Style/UnlessLogicalOperators
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Disabled |
Yes |
No |
1.11 |
- |
This cop checks for the use of logical operators in an unless
condition.
It discourages such code, as the condition becomes more difficult
to read and understand.
This cop supports two styles:
- forbid_mixed_logical_operators
(default)
- forbid_logical_operators
forbid_mixed_logical_operators
style forbids the use of more than one type
of logical operators. This makes the unless
condition easier to read
because either all conditions need to be met or any condition need to be met
in order for the expression to be truthy or falsey.
forbid_logical_operators
style forbids any use of logical operator.
This makes it even more easy to read the unless
condition as
there is only one condition in the expression.
Examples
EnforcedStyle: forbid_mixed_logical_operators (default)
# bad
return unless a || b && c
return unless a && b || c
return unless a && b and c
return unless a || b or c
return unless a && b or c
return unless a || b and c
# good
return unless a && b && c
return unless a || b || c
return unless a and b and c
return unless a or b or c
return unless a?
Style/UnpackFirst
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.54 |
- |
This cop checks for accessing the first element of String#unpack
which can be replaced with the shorter method unpack1
.
Style/VariableInterpolation
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.9 |
0.20 |
This cop checks for variable interpolation (like "#@ivar").
Examples
# bad
"His name is #$name"
/check #$pattern/
"Let's go to the #@store"
# good
"His name is #{$name}"
/check #{$pattern}/
"Let's go to the #{@store}"
Style/WhenThen
Style/WhileUntilDo
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.9 |
- |
Checks for uses of do
in multi-line while/until
statements.
Style/WhileUntilModifier
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.9 |
0.30 |
Checks for while and until statements that would fit on one line
if written as a modifier while/until. The maximum line length is
configured in the Layout/LineLength
cop.
Examples
# bad
while x < 10
x += 1
end
# good
x += 1 while x < 10
# bad
until x > 10
x += 1
end
# good
x += 1 until x > 10
# bad
x += 100 while x < 500 # a long comment that makes code too long if it were a single line
# good
while x < 500 # a long comment that makes code too long if it were a single line
x += 100
end
Style/WordArray
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.9 |
1.19 |
This cop can check for array literals made up of word-like strings, that are not using the %w() syntax.
Alternatively, it can check for uses of the %w() syntax, in projects which do not want to include that syntax.
When using the percent style, %w() arrays containing a space
will be registered as offenses.
|
Configuration option: MinSize
If set, arrays with fewer elements than this value will not trigger the
cop. For example, a MinSize
of 3
will not enforce a style on an
array of 2 or fewer elements.
Examples
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle |
|
|
MinSize |
|
Integer |
WordRegex |
`(?-mix:\A(?:\p{Word} |
\p{Word}-\p{Word} |
\n |
\t)+\z)` |
Style/YodaCondition
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
No |
Yes (Unsafe) |
0.49 |
0.75 |
This cop can either enforce or forbid Yoda conditions,
i.e. comparison operations where the order of expression is reversed.
eg. 5 == x
Safety
This cop is unsafe because comparison operators can be defined differently on different classes, and are not guaranteed to have the same result if reversed.
For example:
class MyKlass
def ==(other)
true
end
end
obj = MyKlass.new
obj == 'string' #=> true
'string' == obj #=> false
Examples
EnforcedStyle: forbid_for_all_comparison_operators (default)
# bad
99 == foo
"bar" != foo
42 >= foo
10 < bar
# good
foo == 99
foo == "bar"
foo <= 42
bar > 10
"#{interpolation}" == foo
/#{interpolation}/ == foo
EnforcedStyle: forbid_for_equality_operators_only
# bad
99 == foo
"bar" != foo
# good
99 >= foo
3 < a && a < 5
Style/ZeroLengthPredicate
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
No |
Yes (Unsafe) |
0.37 |
0.39 |
This cop checks for numeric comparisons that can be replaced by a predicate method, such as receiver.length == 0, receiver.length > 0, receiver.length != 0, receiver.length < 1 and receiver.size == 0 that can be replaced by receiver.empty? and !receiver.empty?.