Layout
Layout/AccessModifierIndentation
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
Bare access modifiers (those not applying to specific methods) should be indented as deep as method definitions, or as deep as the class/module keyword, depending on configuration.
Examples
Layout/ArgumentAlignment
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.68 |
0.77 |
Here we check if the arguments on a multi-line method definition are aligned.
Examples
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle |
|
|
IndentationWidth |
|
Integer |
Layout/ArrayAlignment
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
0.77 |
Here we check if the elements of a multi-line array literal are aligned.
Examples
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle |
|
|
IndentationWidth |
|
Integer |
Layout/AssignmentIndentation
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
0.77 |
This cop checks the indentation of the first line of the right-hand-side of a multi-line assignment.
The indentation of the remaining lines can be corrected with
other cops such as IndentationConsistency
and EndAlignment
.
Layout/BeginEndAlignment
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.91 |
- |
This cop checks whether the end keyword of begin
is aligned properly.
Two modes are supported through the EnforcedStyleAlignWith
configuration
parameter. If it’s set to start_of_line
(which is the default), the
end
shall be aligned with the start of the line where the begin
keyword is. If it’s set to begin
, the end
shall be aligned with the
begin
keyword.
Layout/EndAlignment
cop aligns with keywords (e.g. if
, while
, case
)
by default. On the other hand, ||= begin
that this cop targets tends to
align with the start of the line, it defaults to EnforcedStyleAlignWith: start_of_line
.
These style can be configured by each cop.
Layout/BlockAlignment
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.53 |
- |
This cop checks whether the end keywords are aligned properly for do end blocks.
Three modes are supported through the EnforcedStyleAlignWith
configuration parameter:
start_of_block
: the end
shall be aligned with the
start of the line where the do
appeared.
start_of_line
: the end
shall be aligned with the
start of the line where the expression started.
either
(which is the default) : the end
is allowed to be in either
location. The autofixer will default to start_of_line
.
Layout/BlockEndNewline
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
This cop checks whether the end statement of a do..end block is on its own line.
Layout/CaseIndentation
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
This cop checks how the when
s of a case
expression
are indented in relation to its case
or end
keyword.
It will register a separate offense for each misaligned when
.
Examples
# If Layout/EndAlignment is set to keyword style (default)
# *case* and *end* should always be aligned to same depth,
# and therefore *when* should always be aligned to both -
# regardless of configuration.
# bad for all styles
case n
when 0
x * 2
else
y / 3
end
# good for all styles
case n
when 0
x * 2
else
y / 3
end
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle |
|
|
IndentOneStep |
|
Boolean |
IndentationWidth |
|
Integer |
Layout/ClassStructure
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Disabled |
Yes |
Yes |
0.52 |
- |
Checks if the code style follows the ExpectedOrder configuration:
Categories
allows us to map macro names into a category.
Consider an example of code style that covers the following order:
-
Module inclusion (include, prepend, extend)
-
Constants
-
Associations (has_one, has_many)
-
Public attribute macros (attr_accessor, attr_writer, attr_reader)
-
Other macros (validates, validate)
-
Public class methods
-
Initializer
-
Public instance methods
-
Protected attribute macros (attr_accessor, attr_writer, attr_reader)
-
Protected instance methods
-
Private attribute macros (attr_accessor, attr_writer, attr_reader)
-
Private instance methods
You can configure the following order:
Layout/ClassStructure:
ExpectedOrder:
- module_inclusion
- constants
- association
- public_attribute_macros
- public_delegate
- macros
- public_class_methods
- initializer
- public_methods
- protected_attribute_macros
- protected_methods
- private_attribute_macros
- private_delegate
- private_methods
Instead of putting all literals in the expected order, is also possible to group categories of macros. Visibility levels are handled automatically.
Layout/ClassStructure:
Categories:
association:
- has_many
- has_one
attribute_macros:
- attr_accessor
- attr_reader
- attr_writer
macros:
- validates
- validate
module_inclusion:
- include
- prepend
- extend
Examples
# bad
# Expect extend be before constant
class Person < ApplicationRecord
has_many :orders
ANSWER = 42
extend SomeModule
include AnotherModule
end
# good
class Person
# extend and include go first
extend SomeModule
include AnotherModule
# inner classes
CustomError = Class.new(StandardError)
# constants are next
SOME_CONSTANT = 20
# afterwards we have public attribute macros
attr_reader :name
# followed by other macros (if any)
validates :name
# then we have public delegate macros
delegate :to_s, to: :name
# public class methods are next in line
def self.some_method
end
# initialization goes between class methods and instance methods
def initialize
end
# followed by other public instance methods
def some_method
end
# protected attribute macros and methods go next
protected
attr_reader :protected_name
def some_protected_method
end
# private attribute macros, delegate macros and methods
# are grouped near the end
private
attr_reader :private_name
delegate :some_private_delegate, to: :name
def some_private_method
end
end
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
Categories |
|
|
ExpectedOrder |
|
Array |
Layout/ClosingHeredocIndentation
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.57 |
- |
Checks the indentation of here document closings.
Layout/ClosingParenthesisIndentation
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
This cop checks the indentation of hanging closing parentheses in
method calls, method definitions, and grouped expressions. A hanging
closing parenthesis means )
preceded by a line break.
Examples
# bad
some_method(
a,
b
)
some_method(
a, b
)
some_method(a, b, c
)
some_method(a,
b,
c
)
some_method(a,
x: 1,
y: 2
)
# Scenario 1: When First Parameter Is On Its Own Line
# good: when first param is on a new line, right paren is *always*
# outdented by IndentationWidth
some_method(
a,
b
)
# good
some_method(
a, b
)
# Scenario 2: When First Parameter Is On The Same Line
# good: when all other params are also on the same line, outdent
# right paren by IndentationWidth
some_method(a, b, c
)
# good: when all other params are on multiple lines, but are lined
# up, align right paren with left paren
some_method(a,
b,
c
)
# good: when other params are not lined up on multiple lines, outdent
# right paren by IndentationWidth
some_method(a,
x: 1,
y: 2
)
Layout/CommentIndentation
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
This cop checks the indentation of comments.
Layout/ConditionPosition
Layout/DefEndAlignment
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.53 |
- |
This cop checks whether the end keywords of method definitions are aligned properly.
Two modes are supported through the EnforcedStyleAlignWith configuration
parameter. If it’s set to start_of_line
(which is the default), the
end
shall be aligned with the start of the line where the def
keyword is. If it’s set to def
, the end
shall be aligned with the
def
keyword.
Layout/DotPosition
Layout/ElseAlignment
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
This cop checks the alignment of else keywords. Normally they should be aligned with an if/unless/while/until/begin/def/rescue keyword, but there are special cases when they should follow the same rules as the alignment of end.
Layout/EmptyComment
Layout/EmptyLineAfterGuardClause
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.56 |
0.59 |
This cop enforces empty line after guard clause
Layout/EmptyLineAfterMagicComment
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
Checks for a newline after the final magic comment.
Layout/EmptyLineAfterMultilineCondition
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Disabled |
Yes |
Yes |
0.90 |
- |
This cop enforces empty line after multiline condition.
Examples
# bad
if multiline &&
condition
do_something
end
# good
if multiline &&
condition
do_something
end
# bad
case x
when foo,
bar
do_something
end
# good
case x
when foo,
bar
do_something
end
# bad
begin
do_something
rescue FooError,
BarError
handle_error
end
# good
begin
do_something
rescue FooError,
BarError
handle_error
end
Layout/EmptyLineBetweenDefs
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
1.4 |
This cop checks whether class/module/method definitions are separated by one or more empty lines.
NumberOfEmptyLines
can be an integer (default is 1) or
an array (e.g. [1, 2]) to specify a minimum and maximum
number of empty lines permitted.
AllowAdjacentOneLineDefs
configures whether adjacent
one-line definitions are considered an offense.
Examples
EmptyLineBetweenMethodDefs: true (default)
# checks for empty lines between method definitions.
# bad
def a
end
def b
end
# good
def a
end
def b
end
Layout/EmptyLines
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
This cop checks for two or more consecutive blank lines.
Layout/EmptyLinesAroundAccessModifier
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
Access modifiers should be surrounded by blank lines.
Examples
Layout/EmptyLinesAroundArguments
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.52 |
- |
This cop checks if empty lines exist around the arguments of a method invocation.
Layout/EmptyLinesAroundAttributeAccessor
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.83 |
0.84 |
Checks for a newline after an attribute accessor or a group of them.
alias
syntax and alias_method
, public
, protected
, and private
methods are allowed
by default. These are customizable with AllowAliasSyntax
and AllowedMethods
options.
Examples
# bad
attr_accessor :foo
def do_something
end
# good
attr_accessor :foo
def do_something
end
# good
attr_accessor :foo
attr_reader :bar
attr_writer :baz
attr :qux
def do_something
end
Layout/EmptyLinesAroundBeginBody
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
This cop checks if empty lines exist around the bodies of begin-end blocks.
Layout/EmptyLinesAroundBlockBody
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
This cop checks if empty lines around the bodies of blocks match the configuration.
Layout/EmptyLinesAroundClassBody
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
0.53 |
This cop checks if empty lines around the bodies of classes match the configuration.
Layout/EmptyLinesAroundExceptionHandlingKeywords
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
This cop checks if empty lines exist around the bodies of begin
sections. This cop doesn’t check empty lines at begin
body
beginning/end and around method definition body.
Style/EmptyLinesAroundBeginBody
or Style/EmptyLinesAroundMethodBody
can be used for this purpose.
Layout/EmptyLinesAroundMethodBody
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
This cop checks if empty lines exist around the bodies of methods.
Layout/EmptyLinesAroundModuleBody
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
This cop checks if empty lines around the bodies of modules match the configuration.
Layout/EndAlignment
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.53 |
- |
This cop checks whether the end keywords are aligned properly.
Three modes are supported through the EnforcedStyleAlignWith
configuration parameter:
If it’s set to keyword
(which is the default), the end
shall be aligned with the start of the keyword (if, class, etc.).
If it’s set to variable
the end
shall be aligned with the
left-hand-side of the variable assignment, if there is one.
If it’s set to start_of_line
, the end
shall be aligned with the
start of the line where the matching keyword appears.
This Layout/EndAlignment
cop aligns with keywords (e.g. if
, while
, case
)
by default. On the other hand, Layout/BeginEndAlignment
cop aligns with
EnforcedStyleAlignWith: start_of_line
by default due to ||= begin
tends
to align with the start of the line. These style can be configured by each cop.
Layout/EndOfLine
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
No |
0.49 |
- |
This cop checks for Windows-style line endings in the source code.
Examples
EnforcedStyle: native (default)
# The `native` style means that CR+LF (Carriage Return + Line Feed) is
# enforced on Windows, and LF is enforced on other platforms.
# bad
puts 'Hello' # Return character is LF on Windows.
puts 'Hello' # Return character is CR+LF on other than Windows.
# good
puts 'Hello' # Return character is CR+LF on Windows.
puts 'Hello' # Return character is LF on other than Windows.
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle |
|
|
Layout/ExtraSpacing
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
This cop checks for extra/unnecessary whitespace.
Examples
# good if AllowForAlignment is true
name = "RuboCop"
# Some comment and an empty line
website += "/rubocop-hq/rubocop" unless cond
puts "rubocop" if debug
# bad for any configuration
set_app("RuboCop")
website = "https://github.com/rubocop-hq/rubocop"
# good only if AllowBeforeTrailingComments is true
object.method(arg) # this is a comment
# good even if AllowBeforeTrailingComments is false or not set
object.method(arg) # this is a comment
# good with either AllowBeforeTrailingComments or AllowForAlignment
object.method(arg) # this is a comment
another_object.method(arg) # this is another comment
some_object.method(arg) # this is some comment
Layout/FirstArgumentIndentation
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.68 |
0.77 |
This cop checks the indentation of the first argument in a method call. Arguments after the first one are checked by Layout/ArgumentAlignment, not by this cop.
For indenting the first parameter of method definitions, check out Layout/FirstParameterIndentation.
Examples
# bad
some_method(
first_param,
second_param)
foo = some_method(
first_param,
second_param)
foo = some_method(nested_call(
nested_first_param),
second_param)
foo = some_method(
nested_call(
nested_first_param),
second_param)
some_method nested_call(
nested_first_param),
second_param
EnforcedStyle: consistent
# The first argument should always be indented one step more than the
# preceding line.
# good
some_method(
first_param,
second_param)
foo = some_method(
first_param,
second_param)
foo = some_method(nested_call(
nested_first_param),
second_param)
foo = some_method(
nested_call(
nested_first_param),
second_param)
some_method nested_call(
nested_first_param),
second_param
EnforcedStyle: consistent_relative_to_receiver
# The first argument should always be indented one level relative to
# the parent that is receiving the argument
# good
some_method(
first_param,
second_param)
foo = some_method(
first_param,
second_param)
foo = some_method(nested_call(
nested_first_param),
second_param)
foo = some_method(
nested_call(
nested_first_param),
second_param)
some_method nested_call(
nested_first_param),
second_params
EnforcedStyle: special_for_inner_method_call
# The first argument should normally be indented one step more than
# the preceding line, but if it's a argument for a method call that
# is itself a argument in a method call, then the inner argument
# should be indented relative to the inner method.
# good
some_method(
first_param,
second_param)
foo = some_method(
first_param,
second_param)
foo = some_method(nested_call(
nested_first_param),
second_param)
foo = some_method(
nested_call(
nested_first_param),
second_param)
some_method nested_call(
nested_first_param),
second_param
EnforcedStyle: special_for_inner_method_call_in_parentheses (default)
# Same as `special_for_inner_method_call` except that the special rule
# only applies if the outer method call encloses its arguments in
# parentheses.
# good
some_method(
first_param,
second_param)
foo = some_method(
first_param,
second_param)
foo = some_method(nested_call(
nested_first_param),
second_param)
foo = some_method(
nested_call(
nested_first_param),
second_param)
some_method nested_call(
nested_first_param),
second_param
Layout/FirstArrayElementIndentation
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.68 |
0.77 |
This cop checks the indentation of the first element in an array literal where the opening bracket and the first element are on separate lines. The other elements' indentations are handled by the ArrayAlignment cop.
By default, array literals that are arguments in a method call with parentheses, and where the opening square bracket of the array is on the same line as the opening parenthesis of the method call, shall have their first element indented one step (two spaces) more than the position inside the opening parenthesis.
Other array literals shall have their first element indented one step more than the start of the line where the opening square bracket is.
This default style is called 'special_inside_parentheses'. Alternative styles are 'consistent' and 'align_brackets'. Here are examples:
Examples
EnforcedStyle: special_inside_parentheses (default)
# The `special_inside_parentheses` style enforces that the first
# element in an array literal where the opening bracket and first
# element are on separate lines is indented one step (two spaces) more
# than the position inside the opening parenthesis.
#bad
array = [
:value
]
and_in_a_method_call([
:no_difference
])
#good
array = [
:value
]
but_in_a_method_call([
:its_like_this
])
EnforcedStyle: consistent
# The `consistent` style enforces that the first element in an array
# literal where the opening bracket and the first element are on
# separate lines is indented the same as an array literal which is not
# defined inside a method call.
#bad
# consistent
array = [
:value
]
but_in_a_method_call([
:its_like_this
])
#good
array = [
:value
]
and_in_a_method_call([
:no_difference
])
Layout/FirstHashElementIndentation
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.68 |
0.77 |
This cop checks the indentation of the first key in a hash literal where the opening brace and the first key are on separate lines. The other keys' indentations are handled by the HashAlignment cop.
By default, Hash literals that are arguments in a method call with parentheses, and where the opening curly brace of the hash is on the same line as the opening parenthesis of the method call, shall have their first key indented one step (two spaces) more than the position inside the opening parenthesis.
Other hash literals shall have their first key indented one step more than the start of the line where the opening curly brace is.
This default style is called 'special_inside_parentheses'. Alternative styles are 'consistent' and 'align_braces'. Here are examples:
Examples
EnforcedStyle: special_inside_parentheses (default)
# The `special_inside_parentheses` style enforces that the first key
# in a hash literal where the opening brace and the first key are on
# separate lines is indented one step (two spaces) more than the
# position inside the opening parentheses.
# bad
hash = {
key: :value
}
and_in_a_method_call({
no: :difference
})
# good
special_inside_parentheses
hash = {
key: :value
}
but_in_a_method_call({
its_like: :this
})
EnforcedStyle: consistent
# The `consistent` style enforces that the first key in a hash
# literal where the opening brace and the first key are on
# separate lines is indented the same as a hash literal which is not
# defined inside a method call.
# bad
hash = {
key: :value
}
but_in_a_method_call({
its_like: :this
})
# good
hash = {
key: :value
}
and_in_a_method_call({
no: :difference
})
Layout/FirstMethodParameterLineBreak
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Disabled |
Yes |
Yes |
0.49 |
- |
This cop checks for a line break before the first parameter in a multi-line method parameter definition.
Layout/FirstParameterIndentation
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
0.77 |
This cop checks the indentation of the first parameter in a method definition. Parameters after the first one are checked by Layout/ParameterAlignment, not by this cop.
For indenting the first argument of method calls, check out Layout/FirstArgumentIndentation, which supports options related to nesting that are irrelevant for method definitions.
Layout/HashAlignment
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
0.77 |
Check that the keys, separators, and values of a multi-line hash literal are aligned according to configuration. The configuration options are:
-
key (left align keys, one space before hash rockets and values)
-
separator (align hash rockets and colons, right align keys)
-
table (left align keys, hash rockets, and values)
The treatment of hashes passed as the last argument to a method call can also be configured. The options are:
-
always_inspect
-
always_ignore
-
ignore_implicit (without curly braces)
Alternatively you can specify multiple allowed styles. That’s done by passing a list of styles to EnforcedStyles.
Examples
EnforcedHashRocketStyle: key (default)
# bad
{
:foo => bar,
:ba => baz
}
{
:foo => bar,
:ba => baz
}
# good
{
:foo => bar,
:ba => baz
}
EnforcedHashRocketStyle: separator
# bad
{
:foo => bar,
:ba => baz
}
{
:foo => bar,
:ba => baz
}
# good
{
:foo => bar,
:ba => baz
}
EnforcedColonStyle: key (default)
# bad
{
foo: bar,
ba: baz
}
{
foo: bar,
ba: baz
}
# good
{
foo: bar,
ba: baz
}
EnforcedLastArgumentHashStyle: always_inspect (default)
# Inspect both implicit and explicit hashes.
# bad
do_something(foo: 1,
bar: 2)
# bad
do_something({foo: 1,
bar: 2})
# good
do_something(foo: 1,
bar: 2)
# good
do_something(
foo: 1,
bar: 2
)
# good
do_something({foo: 1,
bar: 2})
# good
do_something({
foo: 1,
bar: 2
})
EnforcedLastArgumentHashStyle: always_ignore
# Ignore both implicit and explicit hashes.
# good
do_something(foo: 1,
bar: 2)
# good
do_something({foo: 1,
bar: 2})
Layout/HeredocArgumentClosingParenthesis
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Disabled |
Yes |
Yes |
0.68 |
- |
This cop checks for the placement of the closing parenthesis in a method call that passes a HEREDOC string as an argument. It should be placed at the end of the line containing the opening HEREDOC tag.
Layout/HeredocIndentation
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
0.85 |
This cop checks the indentation of the here document bodies. The bodies are indented one step.
Note: When Layout/LineLength
's AllowHeredoc
is false (not default),
this cop does not add any offenses for long here documents to
avoid `Layout/LineLength’s offenses.
Layout/IndentationConsistency
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
This cop checks for inconsistent indentation.
The difference between indented_internal_methods
and normal
is
that the indented_internal_methods
style prescribes that in
classes and modules the protected
and private
modifier keywords
shall be indented the same as public methods and that protected and
private members shall be indented one step more than the modifiers.
Other than that, both styles mean that entities on the same logical
depth shall have the same indentation.
Examples
EnforcedStyle: normal (default)
# bad
class A
def test
puts 'hello'
puts 'world'
end
end
# bad
class A
def test
puts 'hello'
puts 'world'
end
protected
def foo
end
private
def bar
end
end
# good
class A
def test
puts 'hello'
puts 'world'
end
end
# good
class A
def test
puts 'hello'
puts 'world'
end
protected
def foo
end
private
def bar
end
end
EnforcedStyle: indented_internal_methods
# bad
class A
def test
puts 'hello'
puts 'world'
end
end
# bad
class A
def test
puts 'hello'
puts 'world'
end
protected
def foo
end
private
def bar
end
end
# good
class A
def test
puts 'hello'
puts 'world'
end
end
# good
class A
def test
puts 'hello'
puts 'world'
end
protected
def foo
end
private
def bar
end
end
Layout/IndentationStyle
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
0.82 |
This cop checks that the indentation method is consistent. Either tabs only or spaces only are used for indentation.
Examples
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
IndentationWidth |
|
Integer |
EnforcedStyle |
|
|
Layout/IndentationWidth
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
This cop checks for indentation that doesn’t use the specified number of spaces.
See also the IndentationConsistency cop which is the companion to this one.
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
Width |
|
Integer |
IgnoredPatterns |
|
Array |
Layout/LeadingCommentSpace
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
0.73 |
This cop checks whether comments have a leading space after the
denoting the start of the comment. The leading space is not
required for some RDoc special syntax, like
++
, --
,
:nodoc
, =begin
- and =end
comments, "shebang" directives,
or rackup options.
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
AllowDoxygenCommentStyle |
|
Boolean |
AllowGemfileRubyComment |
|
Boolean |
Layout/LeadingEmptyLines
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.57 |
0.77 |
This cop checks for unnecessary leading blank lines at the beginning of a file.
Layout/LineLength
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.25 |
1.4 |
This cop checks the length of lines in the source code.
The maximum length is configurable.
The tab size is configured in the IndentationWidth
of the Layout/IndentationStyle
cop.
It also ignores a shebang line by default.
This cop has some autocorrection capabilities. It can programmatically shorten certain long lines by inserting line breaks into expressions that can be safely split across lines. These include arrays, hashes, and method calls with argument lists.
If autocorrection is enabled, the following Layout cops are recommended to further format the broken lines. (Many of these are enabled by default.)
-
ArgumentAlignment
-
BlockAlignment
-
BlockDelimiters
-
BlockEndNewline
-
ClosingParenthesisIndentation
-
FirstArgumentIndentation
-
FirstArrayElementIndentation
-
FirstHashElementIndentation
-
FirstParameterIndentation
-
HashAlignment
-
IndentationWidth
-
MultilineArrayLineBreaks
-
MultilineBlockLayout
-
MultilineHashBraceLayout
-
MultilineHashKeyLineBreaks
-
MultilineMethodArgumentLineBreaks
-
ParameterAlignment
Together, these cops will pretty print hashes, arrays, method calls, etc. For example, let’s say the max columns is 25:
Examples
# bad
{foo: "0000000000", bar: "0000000000", baz: "0000000000"}
# good
{foo: "0000000000",
bar: "0000000000", baz: "0000000000"}
# good (with recommended cops enabled)
{
foo: "0000000000",
bar: "0000000000",
baz: "0000000000",
}
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
AutoCorrect |
|
Boolean |
Max |
|
Integer |
AllowHeredoc |
|
Boolean |
AllowURI |
|
Boolean |
URISchemes |
|
Array |
IgnoreCopDirectives |
|
Boolean |
IgnoredPatterns |
|
Array |
Layout/MultilineArrayBraceLayout
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
This cop checks that the closing brace in an array literal is either on the same line as the last array element or on a new line.
When using the symmetrical
(default) style:
If an array’s opening brace is on the same line as the first element of the array, then the closing brace should be on the same line as the last element of the array.
If an array’s opening brace is on the line above the first element of the array, then the closing brace should be on the line below the last element of the array.
When using the new_line
style:
The closing brace of a multi-line array literal must be on the line after the last element of the array.
When using the same_line
style:
The closing brace of a multi-line array literal must be on the same line as the last element of the array.
Layout/MultilineAssignmentLayout
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Disabled |
Yes |
Yes |
0.49 |
- |
This cop checks whether the multiline assignments have a newline after the assignment operator.
Examples
Layout/MultilineBlockLayout
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
This cop checks whether the multiline do end blocks have a newline after the start of the block. Additionally, it checks whether the block arguments, if any, are on the same line as the start of the block. Putting block arguments on separate lines, because the whole line would otherwise be too long, is accepted.
Layout/MultilineHashBraceLayout
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
This cop checks that the closing brace in a hash literal is either on the same line as the last hash element, or a new line.
When using the symmetrical
(default) style:
If a hash’s opening brace is on the same line as the first element of the hash, then the closing brace should be on the same line as the last element of the hash.
If a hash’s opening brace is on the line above the first element of the hash, then the closing brace should be on the line below the last element of the hash.
When using the new_line
style:
The closing brace of a multi-line hash literal must be on the line after the last element of the hash.
When using the same_line
style:
The closing brace of a multi-line hash literal must be on the same line as the last element of the hash.
Layout/MultilineMethodCallBraceLayout
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
This cop checks that the closing brace in a method call is either on the same line as the last method argument, or a new line.
When using the symmetrical
(default) style:
If a method call’s opening brace is on the same line as the first argument of the call, then the closing brace should be on the same line as the last argument of the call.
If an method call’s opening brace is on the line above the first argument of the call, then the closing brace should be on the line below the last argument of the call.
When using the new_line
style:
The closing brace of a multi-line method call must be on the line after the last argument of the call.
When using the same_line
style:
The closing brace of a multi-line method call must be on the same line as the last argument of the call.
Layout/MultilineMethodCallIndentation
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
This cop checks the indentation of the method name part in method calls that span more than one line.
Layout/MultilineMethodDefinitionBraceLayout
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
This cop checks that the closing brace in a method definition is either on the same line as the last method parameter, or a new line.
When using the symmetrical
(default) style:
If a method definition’s opening brace is on the same line as the first parameter of the definition, then the closing brace should be on the same line as the last parameter of the definition.
If an method definition’s opening brace is on the line above the first parameter of the definition, then the closing brace should be on the line below the last parameter of the definition.
When using the new_line
style:
The closing brace of a multi-line method definition must be on the line after the last parameter of the definition.
When using the same_line
style:
The closing brace of a multi-line method definition must be on the same line as the last parameter of the definition.
Layout/MultilineOperationIndentation
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
This cop checks the indentation of the right hand side operand in binary operations that span more than one line.
The aligned
style checks that operators are aligned if they are part
of an if
or while
condition, a return
statement, etc. In other
contexts, the second operand should be indented regardless of enforced
style.
Layout/ParameterAlignment
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
0.77 |
Here we check if the parameters on a multi-line method call or definition are aligned.
To set the alignment of the first argument, use the cop FirstParameterIndentation.
Examples
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle |
|
|
IndentationWidth |
|
Integer |
Layout/SpaceAfterColon
Layout/SpaceAfterComma
Layout/SpaceAfterMethodName
Layout/SpaceAfterNot
Layout/SpaceAfterSemicolon
Layout/SpaceAroundBlockParameters
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
Checks the spacing inside and after block parameters pipes. Line breaks
inside parameter pipes are checked by Layout/MultilineBlockLayout
and
not by this cop.
Layout/SpaceAroundEqualsInParameterDefault
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
Checks that the equals signs in parameter default assignments have or don’t have surrounding space depending on configuration.
Examples
Layout/SpaceAroundKeyword
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
Checks the spacing around the keywords.
Layout/SpaceAroundMethodCallOperator
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.82 |
- |
Checks method call operators to not have spaces around them.
Layout/SpaceAroundOperators
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
Checks that operators have space around them, except for ** which should or shouldn’t have surrounding space depending on configuration. It allows vertical alignment consisting of one or more whitespace around operators.
This cop has AllowForAlignment
option. When true
, allows most
uses of extra spacing if the intent is to align with an operator on
the previous or next line, not counting empty lines or comment lines.
Examples
# bad
total = 3*4
"apple"+"juice"
my_number = 38/4
# good
total = 3 * 4
"apple" + "juice"
my_number = 38 / 4
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
AllowForAlignment |
|
Boolean |
EnforcedStyleForExponentOperator |
|
|
Layout/SpaceBeforeBlockBraces
Layout/SpaceBeforeFirstArg
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
Checks that exactly one space is used between a method name and the first argument for method calls without parentheses.
Alternatively, extra spaces can be added to align the argument with something on a preceding or following line, if the AllowForAlignment config parameter is true.
Layout/SpaceInLambdaLiteral
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
This cop checks for spaces between →
and opening parameter
parenthesis ((
) in lambda literals.
Layout/SpaceInsideArrayLiteralBrackets
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.52 |
- |
Checks that brackets used for array literals have or don’t have surrounding space depending on configuration.
Examples
EnforcedStyle: space
# The `space` style enforces that array literals have
# surrounding space.
# bad
array = [a, b, c, d]
# good
array = [ a, b, c, d ]
EnforcedStyle: no_space (default)
# The `no_space` style enforces that array literals have
# no surrounding space.
# bad
array = [ a, b, c, d ]
# good
array = [a, b, c, d]
EnforcedStyle: compact
# The `compact` style normally requires a space inside
# array brackets, with the exception that successive left
# or right brackets are collapsed together in nested arrays.
# bad
array = [ a, [ b, c ] ]
array = [
[ a ],
[ b, c ]
]
# good
array = [ a, [ b, c ]]
array = [[ a ],
[ b, c ]]
Layout/SpaceInsideBlockBraces
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
Checks that block braces have or don’t have surrounding space inside them on configuration. For blocks taking parameters, it checks that the left brace has or doesn’t have trailing space depending on configuration.
Examples
EnforcedStyle: space (default)
# The `space` style enforces that block braces have
# surrounding space.
# bad
some_array.each {puts e}
# good
some_array.each { puts e }
EnforcedStyle: no_space
# The `no_space` style enforces that block braces don't
# have surrounding space.
# bad
some_array.each { puts e }
# good
some_array.each {puts e}
EnforcedStyleForEmptyBraces: no_space (default)
# The `no_space` EnforcedStyleForEmptyBraces style enforces that
# block braces don't have a space in between when empty.
# bad
some_array.each { }
some_array.each { }
some_array.each { }
# good
some_array.each {}
EnforcedStyleForEmptyBraces: space
# The `space` EnforcedStyleForEmptyBraces style enforces that
# block braces have at least a space in between when empty.
# bad
some_array.each {}
# good
some_array.each { }
some_array.each { }
some_array.each { }
Layout/SpaceInsideHashLiteralBraces
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
Checks that braces used for hash literals have or don’t have surrounding space depending on configuration.
Examples
EnforcedStyle: space (default)
# The `space` style enforces that hash literals have
# surrounding space.
# bad
h = {a: 1, b: 2}
# good
h = { a: 1, b: 2 }
EnforcedStyle: no_space
# The `no_space` style enforces that hash literals have
# no surrounding space.
# bad
h = { a: 1, b: 2 }
# good
h = {a: 1, b: 2}
EnforcedStyle: compact
# The `compact` style normally requires a space inside
# hash braces, with the exception that successive left
# braces or right braces are collapsed together in nested hashes.
# bad
h = { a: { b: 2 } }
foo = { { a: 1 } => { b: { c: 2 } } }
# good
h = { a: { b: 2 }}
foo = {{ a: 1 } => { b: { c: 2 }}}
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle |
|
|
EnforcedStyleForEmptyBraces |
|
|
Layout/SpaceInsideParens
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
0.55 |
Checks for spaces inside ordinary round parentheses.
Examples
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle |
|
|
Layout/SpaceInsideRangeLiteral
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
Checks for spaces inside range literals.
Layout/SpaceInsideReferenceBrackets
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.52 |
0.53 |
Checks that reference brackets have or don’t have surrounding space depending on configuration.
Examples
EnforcedStyle: no_space (default)
# The `no_space` style enforces that reference brackets have
# no surrounding space.
# bad
hash[ :key ]
array[ index ]
# good
hash[:key]
array[index]
EnforcedStyle: space
# The `space` style enforces that reference brackets have
# surrounding space.
# bad
hash[:key]
array[index]
# good
hash[ :key ]
array[ index ]
Layout/SpaceInsideStringInterpolation
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
- |
This cop checks for whitespace within string interpolations.
Examples
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle |
|
|
Layout/TrailingEmptyLines
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
0.77 |
This cop looks for trailing blank lines and a final newline in the source code.
Examples
Configurable attributes
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle |
|
|
Layout/TrailingWhitespace
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged |
---|---|---|---|---|
Enabled |
Yes |
Yes |
0.49 |
1.0 |
This cop looks for trailing whitespace in the source code.
Examples
# The line in this example contains spaces after the 0.
# bad
x = 0
# The line in this example ends directly after the 0.
# good
x = 0