docs/modules/ROOT/pages/cops_layout.adoc
////
Do NOT edit this file by hand directly, as it is automatically generated.
Please make any necessary changes to the cop documentation within the source files themselves.
////
= Layout
[#layoutaccessmodifierindentation]
== Layout/AccessModifierIndentation
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 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-layoutaccessmodifierindentation]
=== Examples
[#enforcedstyle_-indent-_default_-layoutaccessmodifierindentation]
==== EnforcedStyle: indent (default)
[source,ruby]
----
# bad
class Plumbus
private
def smooth; end
end
# good
class Plumbus
private
def smooth; end
end
----
[#enforcedstyle_-outdent-layoutaccessmodifierindentation]
==== EnforcedStyle: outdent
[source,ruby]
----
# bad
class Plumbus
private
def smooth; end
end
# good
class Plumbus
private
def smooth; end
end
----
[#configurable-attributes-layoutaccessmodifierindentation]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `indent`
| `outdent`, `indent`
| IndentationWidth
| `<none>`
| Integer
|===
[#references-layoutaccessmodifierindentation]
=== References
* https://rubystyle.guide#indent-public-private-protected
[#layoutargumentalignment]
== Layout/ArgumentAlignment
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.68
| 0.77
|===
Here we check if the arguments on a multi-line method
definition are aligned.
[#examples-layoutargumentalignment]
=== Examples
[#enforcedstyle_-with_first_argument-_default_-layoutargumentalignment]
==== EnforcedStyle: with_first_argument (default)
[source,ruby]
----
# good
foo :bar,
:baz,
key: value
foo(
:bar,
:baz,
key: value
)
# bad
foo :bar,
:baz,
key: value
foo(
:bar,
:baz,
key: value
)
----
[#enforcedstyle_-with_fixed_indentation-layoutargumentalignment]
==== EnforcedStyle: with_fixed_indentation
[source,ruby]
----
# good
foo :bar,
:baz,
key: value
# bad
foo :bar,
:baz,
key: value
----
[#configurable-attributes-layoutargumentalignment]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `with_first_argument`
| `with_first_argument`, `with_fixed_indentation`
| IndentationWidth
| `<none>`
| Integer
|===
[#references-layoutargumentalignment]
=== References
* https://rubystyle.guide#no-double-indent
[#layoutarrayalignment]
== Layout/ArrayAlignment
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| 0.77
|===
Here we check if the elements of a multi-line array literal are
aligned.
[#examples-layoutarrayalignment]
=== Examples
[#enforcedstyle_-with_first_element-_default_-layoutarrayalignment]
==== EnforcedStyle: with_first_element (default)
[source,ruby]
----
# good
array = [1, 2, 3,
4, 5, 6]
array = ['run',
'forrest',
'run']
# bad
array = [1, 2, 3,
4, 5, 6]
array = ['run',
'forrest',
'run']
----
[#enforcedstyle_-with_fixed_indentation-layoutarrayalignment]
==== EnforcedStyle: with_fixed_indentation
[source,ruby]
----
# good
array = [1, 2, 3,
4, 5, 6]
# bad
array = [1, 2, 3,
4, 5, 6]
----
[#configurable-attributes-layoutarrayalignment]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `with_first_element`
| `with_first_element`, `with_fixed_indentation`
| IndentationWidth
| `<none>`
| Integer
|===
[#references-layoutarrayalignment]
=== References
* https://rubystyle.guide#no-double-indent
[#layoutassignmentindentation]
== Layout/AssignmentIndentation
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| 1.45
|===
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 `Layout/IndentationConsistency` and `Layout/EndAlignment`.
[#examples-layoutassignmentindentation]
=== Examples
[source,ruby]
----
# bad
value =
if foo
'bar'
end
# good
value =
if foo
'bar'
end
----
[#configurable-attributes-layoutassignmentindentation]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| IndentationWidth
| `<none>`
| Integer
|===
[#layoutbeginendalignment]
== Layout/BeginEndAlignment
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.91
| -
|===
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.
[#examples-layoutbeginendalignment]
=== Examples
[#enforcedstylealignwith_-start_of_line-_default_-layoutbeginendalignment]
==== EnforcedStyleAlignWith: start_of_line (default)
[source,ruby]
----
# bad
foo ||= begin
do_something
end
# good
foo ||= begin
do_something
end
----
[#enforcedstylealignwith_-begin-layoutbeginendalignment]
==== EnforcedStyleAlignWith: begin
[source,ruby]
----
# bad
foo ||= begin
do_something
end
# good
foo ||= begin
do_something
end
----
[#configurable-attributes-layoutbeginendalignment]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyleAlignWith
| `start_of_line`
| `start_of_line`, `begin`
| Severity
| `warning`
| String
|===
[#layoutblockalignment]
== Layout/BlockAlignment
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.53
| -
|===
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 autocorrect will default to `start_of_line`.
[#examples-layoutblockalignment]
=== Examples
[#enforcedstylealignwith_-either-_default_-layoutblockalignment]
==== EnforcedStyleAlignWith: either (default)
[source,ruby]
----
# bad
foo.bar
.each do
baz
end
# good
foo.bar
.each do
baz
end
----
[#enforcedstylealignwith_-start_of_block-layoutblockalignment]
==== EnforcedStyleAlignWith: start_of_block
[source,ruby]
----
# bad
foo.bar
.each do
baz
end
# good
foo.bar
.each do
baz
end
----
[#enforcedstylealignwith_-start_of_line-layoutblockalignment]
==== EnforcedStyleAlignWith: start_of_line
[source,ruby]
----
# bad
foo.bar
.each do
baz
end
# good
foo.bar
.each do
baz
end
----
[#configurable-attributes-layoutblockalignment]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyleAlignWith
| `either`
| `either`, `start_of_block`, `start_of_line`
|===
[#layoutblockendnewline]
== Layout/BlockEndNewline
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
Checks whether the end statement of a do..end block
is on its own line.
[#examples-layoutblockendnewline]
=== Examples
[source,ruby]
----
# bad
blah do |i|
foo(i) end
# good
blah do |i|
foo(i)
end
# bad
blah { |i|
foo(i) }
# good
blah { |i|
foo(i)
}
----
[#layoutcaseindentation]
== Layout/CaseIndentation
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| 1.16
|===
Checks how the `when` and ``in``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` and `in`.
[#examples-layoutcaseindentation]
=== Examples
[source,ruby]
----
# 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
case n
in pattern
x * 2
else
y / 3
end
# good for all styles
case n
when 0
x * 2
else
y / 3
end
case n
in pattern
x * 2
else
y / 3
end
----
[#enforcedstyle_-case-_default_-layoutcaseindentation]
==== EnforcedStyle: case (default)
[source,ruby]
----
# if EndAlignment is set to other style such as
# start_of_line (as shown below), then *when* alignment
# configuration does have an effect.
# bad
a = case n
when 0
x * 2
else
y / 3
end
a = case n
in pattern
x * 2
else
y / 3
end
# good
a = case n
when 0
x * 2
else
y / 3
end
a = case n
in pattern
x * 2
else
y / 3
end
----
[#enforcedstyle_-end-layoutcaseindentation]
==== EnforcedStyle: end
[source,ruby]
----
# bad
a = case n
when 0
x * 2
else
y / 3
end
a = case n
in pattern
x * 2
else
y / 3
end
# good
a = case n
when 0
x * 2
else
y / 3
end
a = case n
in pattern
x * 2
else
y / 3
end
----
[#configurable-attributes-layoutcaseindentation]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `case`
| `case`, `end`
| IndentOneStep
| `false`
| Boolean
| IndentationWidth
| `<none>`
| Integer
|===
[#references-layoutcaseindentation]
=== References
* https://rubystyle.guide#indent-when-to-case
[#layoutclassstructure]
== Layout/ClassStructure
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Disabled
| Yes
| Always (Unsafe)
| 0.52
| 1.53
|===
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:
[source,yaml]
----
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.
[source,yaml]
----
Layout/ClassStructure:
Categories:
association:
- has_many
- has_one
attribute_macros:
- attr_accessor
- attr_reader
- attr_writer
macros:
- validates
- validate
module_inclusion:
- include
- prepend
- extend
----
[#safety-layoutclassstructure]
=== Safety
Autocorrection is unsafe because class methods and module inclusion
can behave differently, based on which methods or constants have
already been defined.
Constants will only be moved when they are assigned with literals.
[#examples-layoutclassstructure]
=== Examples
[source,ruby]
----
# 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-layoutclassstructure]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| Categories
| `{"module_inclusion"=>["include", "prepend", "extend"]}`
|
| ExpectedOrder
| `module_inclusion`, `constants`, `public_class_methods`, `initializer`, `public_methods`, `protected_methods`, `private_methods`
| Array
|===
[#references-layoutclassstructure]
=== References
* https://rubystyle.guide#consistent-classes
[#layoutclosingheredocindentation]
== Layout/ClosingHeredocIndentation
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.57
| -
|===
Checks the indentation of here document closings.
[#examples-layoutclosingheredocindentation]
=== Examples
[source,ruby]
----
# bad
class Foo
def bar
<<~SQL
'Hi'
SQL
end
end
# good
class Foo
def bar
<<~SQL
'Hi'
SQL
end
end
# bad
# heredoc contents is before closing heredoc.
foo arg,
<<~EOS
Hi
EOS
# good
foo arg,
<<~EOS
Hi
EOS
# good
foo arg,
<<~EOS
Hi
EOS
----
[#layoutclosingparenthesisindentation]
== Layout/ClosingParenthesisIndentation
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
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-layoutclosingparenthesisindentation]
=== Examples
[source,ruby]
----
# 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
)
----
[#layoutcommentindentation]
== Layout/CommentIndentation
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| 1.24
|===
Checks the indentation of comments.
[#examples-layoutcommentindentation]
=== Examples
[source,ruby]
----
# bad
# comment here
def method_name
end
# comment here
a = 'hello'
# yet another comment
if true
true
end
# good
# comment here
def method_name
end
# comment here
a = 'hello'
# yet another comment
if true
true
end
----
[#allowforalignment_-false-_default_-layoutcommentindentation]
==== AllowForAlignment: false (default)
[source,ruby]
----
# bad
a = 1 # A really long comment
# spanning two lines.
# good
# A really long comment spanning one line.
a = 1
----
[#allowforalignment_-true-layoutcommentindentation]
==== AllowForAlignment: true
[source,ruby]
----
# good
a = 1 # A really long comment
# spanning two lines.
----
[#configurable-attributes-layoutcommentindentation]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| AllowForAlignment
| `false`
| Boolean
|===
[#layoutconditionposition]
== Layout/ConditionPosition
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.53
| 0.83
|===
Checks for conditions that are not on the same line as
if/while/until.
[#examples-layoutconditionposition]
=== Examples
[source,ruby]
----
# bad
if
some_condition
do_something
end
# good
if some_condition
do_something
end
----
[#references-layoutconditionposition]
=== References
* https://rubystyle.guide#same-line-condition
[#layoutdefendalignment]
== Layout/DefEndAlignment
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.53
| -
|===
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.
[#examples-layoutdefendalignment]
=== Examples
[#enforcedstylealignwith_-start_of_line-_default_-layoutdefendalignment]
==== EnforcedStyleAlignWith: start_of_line (default)
[source,ruby]
----
# bad
private def foo
end
# good
private def foo
end
----
[#enforcedstylealignwith_-def-layoutdefendalignment]
==== EnforcedStyleAlignWith: def
[source,ruby]
----
# bad
private def foo
end
# good
private def foo
end
----
[#configurable-attributes-layoutdefendalignment]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyleAlignWith
| `start_of_line`
| `start_of_line`, `def`
| Severity
| `warning`
| String
|===
[#layoutdotposition]
== Layout/DotPosition
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
Checks the . position in multi-line method calls.
[#examples-layoutdotposition]
=== Examples
[#enforcedstyle_-leading-_default_-layoutdotposition]
==== EnforcedStyle: leading (default)
[source,ruby]
----
# bad
something.
method
# good
something
.method
----
[#enforcedstyle_-trailing-layoutdotposition]
==== EnforcedStyle: trailing
[source,ruby]
----
# bad
something
.method
# good
something.
method
----
[#configurable-attributes-layoutdotposition]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `leading`
| `leading`, `trailing`
|===
[#references-layoutdotposition]
=== References
* https://rubystyle.guide#consistent-multi-line-chains
[#layoutelsealignment]
== Layout/ElseAlignment
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
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.
[#examples-layoutelsealignment]
=== Examples
[source,ruby]
----
# bad
if something
code
else
code
end
# bad
if something
code
elsif something
code
end
# good
if something
code
else
code
end
----
[#layoutemptycomment]
== Layout/EmptyComment
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Command-line only
| 0.53
| 1.61
|===
Checks empty comment.
[#examples-layoutemptycomment]
=== Examples
[source,ruby]
----
# bad
#
class Foo
end
# good
#
# Description of `Foo` class.
#
class Foo
end
----
[#allowbordercomment_-true-_default_-layoutemptycomment]
==== AllowBorderComment: true (default)
[source,ruby]
----
# good
def foo
end
#################
def bar
end
----
[#allowbordercomment_-false-layoutemptycomment]
==== AllowBorderComment: false
[source,ruby]
----
# bad
def foo
end
#################
def bar
end
----
[#allowmargincomment_-true-_default_-layoutemptycomment]
==== AllowMarginComment: true (default)
[source,ruby]
----
# good
#
# Description of `Foo` class.
#
class Foo
end
----
[#allowmargincomment_-false-layoutemptycomment]
==== AllowMarginComment: false
[source,ruby]
----
# bad
#
# Description of `Foo` class.
#
class Foo
end
----
[#configurable-attributes-layoutemptycomment]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| AllowBorderComment
| `true`
| Boolean
| AllowMarginComment
| `true`
| Boolean
|===
[#layoutemptylineafterguardclause]
== Layout/EmptyLineAfterGuardClause
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.56
| 0.59
|===
Enforces empty line after guard clause.
This cop allows `# :nocov:` directive after guard clause because
SimpleCov excludes code from the coverage report by wrapping it in `# :nocov:`:
[source,ruby]
----
def foo
# :nocov:
return if condition
# :nocov:
bar
end
----
Refer to SimpleCov's documentation for more details:
https://github.com/simplecov-ruby/simplecov#ignoringskipping-code
[#examples-layoutemptylineafterguardclause]
=== Examples
[source,ruby]
----
# bad
def foo
return if need_return?
bar
end
# good
def foo
return if need_return?
bar
end
# good
def foo
return if something?
return if something_different?
bar
end
# also good
def foo
if something?
do_something
return if need_return?
end
end
----
[#layoutemptylineaftermagiccomment]
== Layout/EmptyLineAfterMagicComment
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
Checks for a newline after the final magic comment.
[#examples-layoutemptylineaftermagiccomment]
=== Examples
[source,ruby]
----
# good
# frozen_string_literal: true
# Some documentation for Person
class Person
# Some code
end
# bad
# frozen_string_literal: true
# Some documentation for Person
class Person
# Some code
end
----
[#references-layoutemptylineaftermagiccomment]
=== References
* https://rubystyle.guide#separate-magic-comments-from-code
[#layoutemptylineaftermultilinecondition]
== Layout/EmptyLineAfterMultilineCondition
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Disabled
| Yes
| Always
| 0.90
| -
|===
Enforces empty line after multiline condition.
[#examples-layoutemptylineaftermultilinecondition]
=== Examples
[source,ruby]
----
# 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
----
[#references-layoutemptylineaftermultilinecondition]
=== References
* https://github.com/airbnb/ruby#multiline-if-newline
[#layoutemptylinebetweendefs]
== Layout/EmptyLineBetweenDefs
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| 1.23
|===
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-layoutemptylinebetweendefs]
=== Examples
[#emptylinebetweenmethoddefs_-true-_default_-layoutemptylinebetweendefs]
==== EmptyLineBetweenMethodDefs: true (default)
[source,ruby]
----
# checks for empty lines between method definitions.
# bad
def a
end
def b
end
----
[source,ruby]
----
# good
def a
end
def b
end
----
[#emptylinebetweenclassdefs_-true-_default_-layoutemptylinebetweendefs]
==== EmptyLineBetweenClassDefs: true (default)
[source,ruby]
----
# checks for empty lines between class definitions.
# bad
class A
end
class B
end
def b
end
----
[source,ruby]
----
# good
class A
end
class B
end
def b
end
----
[#emptylinebetweenmoduledefs_-true-_default_-layoutemptylinebetweendefs]
==== EmptyLineBetweenModuleDefs: true (default)
[source,ruby]
----
# checks for empty lines between module definitions.
# bad
module A
end
module B
end
def b
end
----
[source,ruby]
----
# good
module A
end
module B
end
def b
end
----
[#allowadjacentonelinedefs_-true-_default_-layoutemptylinebetweendefs]
==== AllowAdjacentOneLineDefs: true (default)
[source,ruby]
----
# good
class ErrorA < BaseError; end
class ErrorB < BaseError; end
class ErrorC < BaseError; end
# good
class ErrorA < BaseError; end
class ErrorB < BaseError; end
class ErrorC < BaseError; end
----
[#allowadjacentonelinedefs_-false-layoutemptylinebetweendefs]
==== AllowAdjacentOneLineDefs: false
[source,ruby]
----
# bad
class ErrorA < BaseError; end
class ErrorB < BaseError; end
class ErrorC < BaseError; end
# good
class ErrorA < BaseError; end
class ErrorB < BaseError; end
class ErrorC < BaseError; end
----
[#configurable-attributes-layoutemptylinebetweendefs]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EmptyLineBetweenMethodDefs
| `true`
| Boolean
| EmptyLineBetweenClassDefs
| `true`
| Boolean
| EmptyLineBetweenModuleDefs
| `true`
| Boolean
| DefLikeMacros
| `[]`
| Array
| AllowAdjacentOneLineDefs
| `true`
| Boolean
| NumberOfEmptyLines
| `1`
| Integer
|===
[#references-layoutemptylinebetweendefs]
=== References
* https://rubystyle.guide#empty-lines-between-methods
[#layoutemptylines]
== Layout/EmptyLines
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
Checks for two or more consecutive blank lines.
[#examples-layoutemptylines]
=== Examples
[source,ruby]
----
# bad - It has two empty lines.
some_method
# one empty line
# two empty lines
some_method
# good
some_method
# one empty line
some_method
----
[#references-layoutemptylines]
=== References
* https://rubystyle.guide#two-or-more-empty-lines
[#layoutemptylinesaroundaccessmodifier]
== Layout/EmptyLinesAroundAccessModifier
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
Access modifiers should be surrounded by blank lines.
[#examples-layoutemptylinesaroundaccessmodifier]
=== Examples
[#enforcedstyle_-around-_default_-layoutemptylinesaroundaccessmodifier]
==== EnforcedStyle: around (default)
[source,ruby]
----
# bad
class Foo
def bar; end
private
def baz; end
end
# good
class Foo
def bar; end
private
def baz; end
end
----
[#enforcedstyle_-only_before-layoutemptylinesaroundaccessmodifier]
==== EnforcedStyle: only_before
[source,ruby]
----
# bad
class Foo
def bar; end
private
def baz; end
end
# good
class Foo
def bar; end
private
def baz; end
end
----
[#configurable-attributes-layoutemptylinesaroundaccessmodifier]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `around`
| `around`, `only_before`
|===
[#references-layoutemptylinesaroundaccessmodifier]
=== References
* https://rubystyle.guide#empty-lines-around-access-modifier
* https://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#follow-the-coding-conventions
[#layoutemptylinesaroundarguments]
== Layout/EmptyLinesAroundArguments
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.52
| -
|===
Checks if empty lines exist around the arguments
of a method invocation.
[#examples-layoutemptylinesaroundarguments]
=== Examples
[source,ruby]
----
# bad
do_something(
foo
)
process(bar,
baz: qux,
thud: fred)
some_method(
[1,2,3],
x: y
)
# good
do_something(
foo
)
process(bar,
baz: qux,
thud: fred)
some_method(
[1,2,3],
x: y
)
----
[#layoutemptylinesaroundattributeaccessor]
== Layout/EmptyLinesAroundAttributeAccessor
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 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-layoutemptylinesaroundattributeaccessor]
=== Examples
[source,ruby]
----
# 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
----
[#allowaliassyntax_-true-_default_-layoutemptylinesaroundattributeaccessor]
==== AllowAliasSyntax: true (default)
[source,ruby]
----
# good
attr_accessor :foo
alias :foo? :foo
def do_something
end
----
[#allowaliassyntax_-false-layoutemptylinesaroundattributeaccessor]
==== AllowAliasSyntax: false
[source,ruby]
----
# bad
attr_accessor :foo
alias :foo? :foo
def do_something
end
# good
attr_accessor :foo
alias :foo? :foo
def do_something
end
----
[#allowedmethods_-__private__-layoutemptylinesaroundattributeaccessor]
==== AllowedMethods: ['private']
[source,ruby]
----
# good
attr_accessor :foo
private :foo
def do_something
end
----
[#configurable-attributes-layoutemptylinesaroundattributeaccessor]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| AllowAliasSyntax
| `true`
| Boolean
| AllowedMethods
| `alias_method`, `public`, `protected`, `private`
| Array
|===
[#references-layoutemptylinesaroundattributeaccessor]
=== References
* https://rubystyle.guide#empty-lines-around-attribute-accessor
[#layoutemptylinesaroundbeginbody]
== Layout/EmptyLinesAroundBeginBody
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
Checks if empty lines exist around the bodies of begin-end
blocks.
[#examples-layoutemptylinesaroundbeginbody]
=== Examples
[source,ruby]
----
# good
begin
# ...
end
# bad
begin
# ...
end
----
[#references-layoutemptylinesaroundbeginbody]
=== References
* https://rubystyle.guide#empty-lines-around-bodies
[#layoutemptylinesaroundblockbody]
== Layout/EmptyLinesAroundBlockBody
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
Checks if empty lines around the bodies of blocks match
the configuration.
[#examples-layoutemptylinesaroundblockbody]
=== Examples
[#enforcedstyle_-no_empty_lines-_default_-layoutemptylinesaroundblockbody]
==== EnforcedStyle: no_empty_lines (default)
[source,ruby]
----
# good
foo do |bar|
# ...
end
----
[#enforcedstyle_-empty_lines-layoutemptylinesaroundblockbody]
==== EnforcedStyle: empty_lines
[source,ruby]
----
# good
foo do |bar|
# ...
end
----
[#configurable-attributes-layoutemptylinesaroundblockbody]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `no_empty_lines`
| `empty_lines`, `no_empty_lines`
|===
[#references-layoutemptylinesaroundblockbody]
=== References
* https://rubystyle.guide#empty-lines-around-bodies
[#layoutemptylinesaroundclassbody]
== Layout/EmptyLinesAroundClassBody
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| 0.53
|===
Checks if empty lines around the bodies of classes match
the configuration.
[#examples-layoutemptylinesaroundclassbody]
=== Examples
[#enforcedstyle_-no_empty_lines-_default_-layoutemptylinesaroundclassbody]
==== EnforcedStyle: no_empty_lines (default)
[source,ruby]
----
# good
class Foo
def bar
# ...
end
end
----
[#enforcedstyle_-empty_lines-layoutemptylinesaroundclassbody]
==== EnforcedStyle: empty_lines
[source,ruby]
----
# good
class Foo
def bar
# ...
end
end
----
[#enforcedstyle_-empty_lines_except_namespace-layoutemptylinesaroundclassbody]
==== EnforcedStyle: empty_lines_except_namespace
[source,ruby]
----
# good
class Foo
class Bar
# ...
end
end
----
[#enforcedstyle_-empty_lines_special-layoutemptylinesaroundclassbody]
==== EnforcedStyle: empty_lines_special
[source,ruby]
----
# good
class Foo
def bar; end
end
----
[#enforcedstyle_-beginning_only-layoutemptylinesaroundclassbody]
==== EnforcedStyle: beginning_only
[source,ruby]
----
# good
class Foo
def bar
# ...
end
end
----
[#enforcedstyle_-ending_only-layoutemptylinesaroundclassbody]
==== EnforcedStyle: ending_only
[source,ruby]
----
# good
class Foo
def bar
# ...
end
end
----
[#configurable-attributes-layoutemptylinesaroundclassbody]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `no_empty_lines`
| `empty_lines`, `empty_lines_except_namespace`, `empty_lines_special`, `no_empty_lines`, `beginning_only`, `ending_only`
|===
[#references-layoutemptylinesaroundclassbody]
=== References
* https://rubystyle.guide#empty-lines-around-bodies
[#layoutemptylinesaroundexceptionhandlingkeywords]
== Layout/EmptyLinesAroundExceptionHandlingKeywords
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
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.
[#examples-layoutemptylinesaroundexceptionhandlingkeywords]
=== Examples
[source,ruby]
----
# good
begin
do_something
rescue
do_something2
else
do_something3
ensure
do_something4
end
# good
def foo
do_something
rescue
do_something2
end
# bad
begin
do_something
rescue
do_something2
else
do_something3
ensure
do_something4
end
# bad
def foo
do_something
rescue
do_something2
end
----
[#references-layoutemptylinesaroundexceptionhandlingkeywords]
=== References
* https://rubystyle.guide#empty-lines-around-bodies
[#layoutemptylinesaroundmethodbody]
== Layout/EmptyLinesAroundMethodBody
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
Checks if empty lines exist around the bodies of methods.
[#examples-layoutemptylinesaroundmethodbody]
=== Examples
[source,ruby]
----
# good
def foo
# ...
end
# bad
def bar
# ...
end
----
[#references-layoutemptylinesaroundmethodbody]
=== References
* https://rubystyle.guide#empty-lines-around-bodies
[#layoutemptylinesaroundmodulebody]
== Layout/EmptyLinesAroundModuleBody
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
Checks if empty lines around the bodies of modules match
the configuration.
[#examples-layoutemptylinesaroundmodulebody]
=== Examples
[#enforcedstyle_-no_empty_lines-_default_-layoutemptylinesaroundmodulebody]
==== EnforcedStyle: no_empty_lines (default)
[source,ruby]
----
# good
module Foo
def bar
# ...
end
end
----
[#enforcedstyle_-empty_lines-layoutemptylinesaroundmodulebody]
==== EnforcedStyle: empty_lines
[source,ruby]
----
# good
module Foo
def bar
# ...
end
end
----
[#enforcedstyle_-empty_lines_except_namespace-layoutemptylinesaroundmodulebody]
==== EnforcedStyle: empty_lines_except_namespace
[source,ruby]
----
# good
module Foo
module Bar
# ...
end
end
----
[#enforcedstyle_-empty_lines_special-layoutemptylinesaroundmodulebody]
==== EnforcedStyle: empty_lines_special
[source,ruby]
----
# good
module Foo
def bar; end
end
----
[#configurable-attributes-layoutemptylinesaroundmodulebody]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `no_empty_lines`
| `empty_lines`, `empty_lines_except_namespace`, `empty_lines_special`, `no_empty_lines`
|===
[#references-layoutemptylinesaroundmodulebody]
=== References
* https://rubystyle.guide#empty-lines-around-bodies
[#layoutendalignment]
== Layout/EndAlignment
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.53
| -
|===
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. `Layout/DefEndAlignment` cop also aligns with
`EnforcedStyleAlignWith: start_of_line` by default.
These style can be configured by each cop.
[#examples-layoutendalignment]
=== Examples
[#enforcedstylealignwith_-keyword-_default_-layoutendalignment]
==== EnforcedStyleAlignWith: keyword (default)
[source,ruby]
----
# bad
variable = if true
end
# good
variable = if true
end
variable =
if true
end
----
[#enforcedstylealignwith_-variable-layoutendalignment]
==== EnforcedStyleAlignWith: variable
[source,ruby]
----
# bad
variable = if true
end
# good
variable = if true
end
variable =
if true
end
----
[#enforcedstylealignwith_-start_of_line-layoutendalignment]
==== EnforcedStyleAlignWith: start_of_line
[source,ruby]
----
# bad
variable = if true
end
puts(if true
end)
# good
variable = if true
end
puts(if true
end)
variable =
if true
end
----
[#configurable-attributes-layoutendalignment]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyleAlignWith
| `keyword`
| `keyword`, `variable`, `start_of_line`
| Severity
| `warning`
| String
|===
[#layoutendofline]
== Layout/EndOfLine
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| No
| 0.49
| -
|===
Checks for Windows-style line endings in the source code.
[#examples-layoutendofline]
=== Examples
[#enforcedstyle_-native-_default_-layoutendofline]
==== EnforcedStyle: native (default)
[source,ruby]
----
# 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.
----
[#enforcedstyle_-lf-layoutendofline]
==== EnforcedStyle: lf
[source,ruby]
----
# The `lf` style means that LF (Line Feed) is enforced on
# all platforms.
# bad
puts 'Hello' # Return character is CR+LF on all platforms.
# good
puts 'Hello' # Return character is LF on all platforms.
----
[#enforcedstyle_-crlf-layoutendofline]
==== EnforcedStyle: crlf
[source,ruby]
----
# The `crlf` style means that CR+LF (Carriage Return + Line Feed) is
# enforced on all platforms.
# bad
puts 'Hello' # Return character is LF on all platforms.
# good
puts 'Hello' # Return character is CR+LF on all platforms.
----
[#configurable-attributes-layoutendofline]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `native`
| `native`, `lf`, `crlf`
|===
[#references-layoutendofline]
=== References
* https://rubystyle.guide#crlf
[#layoutextraspacing]
== Layout/ExtraSpacing
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
Checks for extra/unnecessary whitespace.
[#examples-layoutextraspacing]
=== Examples
[source,ruby]
----
# good if AllowForAlignment is true
name = "RuboCop"
# Some comment and an empty line
website += "/rubocop/rubocop" unless cond
puts "rubocop" if debug
# bad for any configuration
set_app("RuboCop")
website = "https://github.com/rubocop/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
----
[#configurable-attributes-layoutextraspacing]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| AllowForAlignment
| `true`
| Boolean
| AllowBeforeTrailingComments
| `false`
| Boolean
| ForceEqualSignAlignment
| `false`
| Boolean
|===
[#layoutfirstargumentindentation]
== Layout/FirstArgumentIndentation
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.68
| 0.77
|===
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`.
This cop will respect `Layout/ArgumentAlignment` and will not work when
`EnforcedStyle: with_fixed_indentation` is specified for `Layout/ArgumentAlignment`.
[#examples-layoutfirstargumentindentation]
=== Examples
[source,ruby]
----
# 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_-special_for_inner_method_call_in_parentheses-_default_-layoutfirstargumentindentation]
==== EnforcedStyle: special_for_inner_method_call_in_parentheses (default)
[source,ruby]
----
# 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
----
[#enforcedstyle_-consistent-layoutfirstargumentindentation]
==== EnforcedStyle: consistent
[source,ruby]
----
# 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-layoutfirstargumentindentation]
==== EnforcedStyle: consistent_relative_to_receiver
[source,ruby]
----
# 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-layoutfirstargumentindentation]
==== EnforcedStyle: special_for_inner_method_call
[source,ruby]
----
# The first argument should normally be indented one step more than
# the preceding line, but if it's an argument for a method call that
# is itself an 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
----
[#configurable-attributes-layoutfirstargumentindentation]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `special_for_inner_method_call_in_parentheses`
| `consistent`, `consistent_relative_to_receiver`, `special_for_inner_method_call`, `special_for_inner_method_call_in_parentheses`
| IndentationWidth
| `<none>`
| Integer
|===
[#layoutfirstarrayelementindentation]
== Layout/FirstArrayElementIndentation
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.68
| 0.77
|===
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 `Layout/ArrayAlignment` cop.
This cop will respect `Layout/ArrayAlignment` and will not work when
`EnforcedStyle: with_fixed_indentation` is specified for `Layout/ArrayAlignment`.
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-layoutfirstarrayelementindentation]
=== Examples
[#enforcedstyle_-special_inside_parentheses-_default_-layoutfirstarrayelementindentation]
==== EnforcedStyle: special_inside_parentheses (default)
[source,ruby]
----
# 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-layoutfirstarrayelementindentation]
==== EnforcedStyle: consistent
[source,ruby]
----
# 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
array = [
:value
]
but_in_a_method_call([
:its_like_this
])
# good
array = [
:value
]
and_in_a_method_call([
:no_difference
])
----
[#enforcedstyle_-align_brackets-layoutfirstarrayelementindentation]
==== EnforcedStyle: align_brackets
[source,ruby]
----
# The `align_brackets` style enforces that the opening and closing
# brackets are indented to the same position.
# bad
and_now_for_something = [
:completely_different
]
# good
and_now_for_something = [
:completely_different
]
----
[#configurable-attributes-layoutfirstarrayelementindentation]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `special_inside_parentheses`
| `special_inside_parentheses`, `consistent`, `align_brackets`
| IndentationWidth
| `<none>`
| Integer
|===
[#layoutfirstarrayelementlinebreak]
== Layout/FirstArrayElementLineBreak
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Disabled
| Yes
| Always
| 0.49
| -
|===
Checks for a line break before the first element in a
multi-line array.
[#examples-layoutfirstarrayelementlinebreak]
=== Examples
[source,ruby]
----
# bad
[ :a,
:b]
# good
[
:a,
:b]
# good
[:a, :b]
----
[#allowmultilinefinalelement_-false-_default_-layoutfirstarrayelementlinebreak]
==== AllowMultilineFinalElement: false (default)
[source,ruby]
----
# bad
[ :a, {
:b => :c
}]
# good
[
:a, {
:b => :c
}]
----
[#allowmultilinefinalelement_-true-layoutfirstarrayelementlinebreak]
==== AllowMultilineFinalElement: true
[source,ruby]
----
# good
[:a, {
:b => :c
}]
----
[#configurable-attributes-layoutfirstarrayelementlinebreak]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| xref:cops_layout.adoc#allowmultilinefinalelement[AllowMultilineFinalElement]
| `false`
| Boolean
|===
[#layoutfirsthashelementindentation]
== Layout/FirstHashElementIndentation
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.68
| 0.77
|===
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-layoutfirsthashelementindentation]
=== Examples
[#enforcedstyle_-special_inside_parentheses-_default_-layoutfirsthashelementindentation]
==== EnforcedStyle: special_inside_parentheses (default)
[source,ruby]
----
# 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
})
takes_multi_pairs_hash(x: {
a: 1,
b: 2
},
y: {
c: 1,
d: 2
})
# good
special_inside_parentheses
hash = {
key: :value
}
but_in_a_method_call({
its_like: :this
})
takes_multi_pairs_hash(x: {
a: 1,
b: 2
},
y: {
c: 1,
d: 2
})
----
[#enforcedstyle_-consistent-layoutfirsthashelementindentation]
==== EnforcedStyle: consistent
[source,ruby]
----
# 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
})
----
[#enforcedstyle_-align_braces-layoutfirsthashelementindentation]
==== EnforcedStyle: align_braces
[source,ruby]
----
# The `align_brackets` style enforces that the opening and closing
# braces are indented to the same position.
# bad
and_now_for_something = {
completely: :different
}
takes_multi_pairs_hash(x: {
a: 1,
b: 2
},
y: {
c: 1,
d: 2
})
# good
and_now_for_something = {
completely: :different
}
takes_multi_pairs_hash(x: {
a: 1,
b: 2
},
y: {
c: 1,
d: 2
})
----
[#configurable-attributes-layoutfirsthashelementindentation]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `special_inside_parentheses`
| `special_inside_parentheses`, `consistent`, `align_braces`
| IndentationWidth
| `<none>`
| Integer
|===
[#layoutfirsthashelementlinebreak]
== Layout/FirstHashElementLineBreak
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Disabled
| Yes
| Always
| 0.49
| -
|===
Checks for a line break before the first element in a
multi-line hash.
[#examples-layoutfirsthashelementlinebreak]
=== Examples
[source,ruby]
----
# bad
{ a: 1,
b: 2}
# good
{
a: 1,
b: 2 }
# good
{
a: 1, b: {
c: 3
}}
----
[#allowmultilinefinalelement_-false-_default_-layoutfirsthashelementlinebreak]
==== AllowMultilineFinalElement: false (default)
[source,ruby]
----
# bad
{ a: 1, b: {
c: 3
}}
----
[#allowmultilinefinalelement_-true-layoutfirsthashelementlinebreak]
==== AllowMultilineFinalElement: true
[source,ruby]
----
# bad
{ a: 1,
b: {
c: 3
}}
# good
{ a: 1, b: {
c: 3
}}
----
[#configurable-attributes-layoutfirsthashelementlinebreak]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| xref:cops_layout.adoc#allowmultilinefinalelement[AllowMultilineFinalElement]
| `false`
| Boolean
|===
[#layoutfirstmethodargumentlinebreak]
== Layout/FirstMethodArgumentLineBreak
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Disabled
| Yes
| Always
| 0.49
| -
|===
Checks for a line break before the first argument in a
multi-line method call.
[#examples-layoutfirstmethodargumentlinebreak]
=== Examples
[source,ruby]
----
# bad
method(foo, bar,
baz)
# good
method(
foo, bar,
baz)
# ignored
method foo, bar,
baz
----
[#allowmultilinefinalelement_-false-_default_-layoutfirstmethodargumentlinebreak]
==== AllowMultilineFinalElement: false (default)
[source,ruby]
----
# bad
method(foo, bar, {
baz: "a",
qux: "b",
})
# good
method(
foo, bar, {
baz: "a",
qux: "b",
})
----
[#allowmultilinefinalelement_-true-layoutfirstmethodargumentlinebreak]
==== AllowMultilineFinalElement: true
[source,ruby]
----
# bad
method(foo,
bar,
{
baz: "a",
qux: "b",
}
)
# good
method(foo, bar, {
baz: "a",
qux: "b",
})
# good
method(
foo,
bar,
{
baz: "a",
qux: "b",
}
)
----
[#allowedmethods_-__some_method__-layoutfirstmethodargumentlinebreak]
==== AllowedMethods: ['some_method']
[source,ruby]
----
# good
some_method(foo, bar,
baz)
----
[#configurable-attributes-layoutfirstmethodargumentlinebreak]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| xref:cops_layout.adoc#allowmultilinefinalelement[AllowMultilineFinalElement]
| `false`
| Boolean
| AllowedMethods
| `[]`
| Array
|===
[#layoutfirstmethodparameterlinebreak]
== Layout/FirstMethodParameterLineBreak
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Disabled
| Yes
| Always
| 0.49
| -
|===
Checks for a line break before the first parameter in a
multi-line method parameter definition.
[#examples-layoutfirstmethodparameterlinebreak]
=== Examples
[source,ruby]
----
# bad
def method(foo, bar,
baz)
do_something
end
# good
def method(
foo, bar,
baz)
do_something
end
# ignored
def method foo,
bar
do_something
end
----
[#allowmultilinefinalelement_-false-_default_-layoutfirstmethodparameterlinebreak]
==== AllowMultilineFinalElement: false (default)
[source,ruby]
----
# bad
def method(foo, bar, baz = {
:a => "b",
})
do_something
end
# good
def method(
foo, bar, baz = {
:a => "b",
})
do_something
end
----
[#allowmultilinefinalelement_-true-layoutfirstmethodparameterlinebreak]
==== AllowMultilineFinalElement: true
[source,ruby]
----
# good
def method(foo, bar, baz = {
:a => "b",
})
do_something
end
----
[#configurable-attributes-layoutfirstmethodparameterlinebreak]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| xref:cops_layout.adoc#allowmultilinefinalelement[AllowMultilineFinalElement]
| `false`
| Boolean
|===
[#layoutfirstparameterindentation]
== Layout/FirstParameterIndentation
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| 0.77
|===
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_.
[#examples-layoutfirstparameterindentation]
=== Examples
[source,ruby]
----
# bad
def some_method(
first_param,
second_param)
123
end
----
[#enforcedstyle_-consistent-_default_-layoutfirstparameterindentation]
==== EnforcedStyle: consistent (default)
[source,ruby]
----
# The first parameter should always be indented one step more than the
# preceding line.
# good
def some_method(
first_param,
second_param)
123
end
----
[#enforcedstyle_-align_parentheses-layoutfirstparameterindentation]
==== EnforcedStyle: align_parentheses
[source,ruby]
----
# The first parameter should always be indented one step more than the
# opening parenthesis.
# good
def some_method(
first_param,
second_param)
123
end
----
[#configurable-attributes-layoutfirstparameterindentation]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `consistent`
| `consistent`, `align_parentheses`
| IndentationWidth
| `<none>`
| Integer
|===
[#layouthashalignment]
== Layout/HashAlignment
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| 1.16
|===
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-layouthashalignment]
=== Examples
[#enforcedhashrocketstyle_-key-_default_-layouthashalignment]
==== EnforcedHashRocketStyle: key (default)
[source,ruby]
----
# bad
{
:foo => bar,
:ba => baz
}
{
:foo => bar,
:ba => baz
}
# good
{
:foo => bar,
:ba => baz
}
----
[#enforcedhashrocketstyle_-separator-layouthashalignment]
==== EnforcedHashRocketStyle: separator
[source,ruby]
----
# bad
{
:foo => bar,
:ba => baz
}
{
:foo => bar,
:ba => baz
}
# good
{
:foo => bar,
:ba => baz
}
----
[#enforcedhashrocketstyle_-table-layouthashalignment]
==== EnforcedHashRocketStyle: table
[source,ruby]
----
# bad
{
:foo => bar,
:ba => baz
}
# good
{
:foo => bar,
:ba => baz
}
----
[#enforcedcolonstyle_-key-_default_-layouthashalignment]
==== EnforcedColonStyle: key (default)
[source,ruby]
----
# bad
{
foo: bar,
ba: baz
}
{
foo: bar,
ba: baz
}
# good
{
foo: bar,
ba: baz
}
----
[#enforcedcolonstyle_-separator-layouthashalignment]
==== EnforcedColonStyle: separator
[source,ruby]
----
# bad
{
foo: bar,
ba: baz
}
# good
{
foo: bar,
ba: baz
}
----
[#enforcedcolonstyle_-table-layouthashalignment]
==== EnforcedColonStyle: table
[source,ruby]
----
# bad
{
foo: bar,
ba: baz
}
# good
{
foo: bar,
ba: baz
}
----
[#enforcedlastargumenthashstyle_-always_inspect-_default_-layouthashalignment]
==== EnforcedLastArgumentHashStyle: always_inspect (default)
[source,ruby]
----
# 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-layouthashalignment]
==== EnforcedLastArgumentHashStyle: always_ignore
[source,ruby]
----
# Ignore both implicit and explicit hashes.
# good
do_something(foo: 1,
bar: 2)
# good
do_something({foo: 1,
bar: 2})
----
[#enforcedlastargumenthashstyle_-ignore_implicit-layouthashalignment]
==== EnforcedLastArgumentHashStyle: ignore_implicit
[source,ruby]
----
# Ignore only implicit hashes.
# bad
do_something({foo: 1,
bar: 2})
# good
do_something(foo: 1,
bar: 2)
----
[#enforcedlastargumenthashstyle_-ignore_explicit-layouthashalignment]
==== EnforcedLastArgumentHashStyle: ignore_explicit
[source,ruby]
----
# Ignore only explicit hashes.
# bad
do_something(foo: 1,
bar: 2)
# good
do_something({foo: 1,
bar: 2})
----
[#configurable-attributes-layouthashalignment]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedHashRocketStyle
| `key`
| `key`, `separator`, `table`
| EnforcedColonStyle
| `key`
| `key`, `separator`, `table`
| EnforcedLastArgumentHashStyle
| `always_inspect`
| `always_inspect`, `always_ignore`, `ignore_implicit`, `ignore_explicit`
|===
[#layoutheredocargumentclosingparenthesis]
== Layout/HeredocArgumentClosingParenthesis
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Disabled
| Yes
| Always
| 0.68
| -
|===
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.
[#examples-layoutheredocargumentclosingparenthesis]
=== Examples
[source,ruby]
----
# bad
foo(<<-SQL
bar
SQL
)
foo(<<-SQL, 123, <<-NOSQL,
bar
SQL
baz
NOSQL
)
foo(
bar(<<-SQL
baz
SQL
),
123,
)
# good
foo(<<-SQL)
bar
SQL
foo(<<-SQL, 123, <<-NOSQL)
bar
SQL
baz
NOSQL
foo(
bar(<<-SQL),
baz
SQL
123,
)
----
[#references-layoutheredocargumentclosingparenthesis]
=== References
* https://rubystyle.guide#heredoc-argument-closing-parentheses
[#layoutheredocindentation]
== Layout/HeredocIndentation
NOTE: Required Ruby version: 2.3
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| 0.85
|===
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.
[#examples-layoutheredocindentation]
=== Examples
[source,ruby]
----
# bad
<<-RUBY
something
RUBY
# good
<<~RUBY
something
RUBY
----
[#references-layoutheredocindentation]
=== References
* https://rubystyle.guide#squiggly-heredocs
[#layoutindentationconsistency]
== Layout/IndentationConsistency
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
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-layoutindentationconsistency]
=== Examples
[#enforcedstyle_-normal-_default_-layoutindentationconsistency]
==== EnforcedStyle: normal (default)
[source,ruby]
----
# 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-layoutindentationconsistency]
==== EnforcedStyle: indented_internal_methods
[source,ruby]
----
# 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
----
[#configurable-attributes-layoutindentationconsistency]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `normal`
| `normal`, `indented_internal_methods`
|===
[#references-layoutindentationconsistency]
=== References
* https://rubystyle.guide#spaces-indentation
* https://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#follow-the-coding-conventions
[#layoutindentationstyle]
== Layout/IndentationStyle
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| 0.82
|===
Checks that the indentation method is consistent.
Either tabs only or spaces only are used for indentation.
[#examples-layoutindentationstyle]
=== Examples
[#enforcedstyle_-spaces-_default_-layoutindentationstyle]
==== EnforcedStyle: spaces (default)
[source,ruby]
----
# bad
# This example uses a tab to indent bar.
def foo
bar
end
# good
# This example uses spaces to indent bar.
def foo
bar
end
----
[#enforcedstyle_-tabs-layoutindentationstyle]
==== EnforcedStyle: tabs
[source,ruby]
----
# bad
# This example uses spaces to indent bar.
def foo
bar
end
# good
# This example uses a tab to indent bar.
def foo
bar
end
----
[#configurable-attributes-layoutindentationstyle]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| IndentationWidth
| `<none>`
| Integer
| EnforcedStyle
| `spaces`
| `spaces`, `tabs`
|===
[#references-layoutindentationstyle]
=== References
* https://rubystyle.guide#spaces-indentation
[#layoutindentationwidth]
== Layout/IndentationWidth
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
Checks for indentation that doesn't use the specified number of spaces.
The indentation width can be configured using the `Width` setting. The default width is 2.
See also the `Layout/IndentationConsistency` cop which is the companion to this one.
[#examples-layoutindentationwidth]
=== Examples
[#width_-2-_default_-layoutindentationwidth]
==== Width: 2 (default)
[source,ruby]
----
# bad
class A
def test
puts 'hello'
end
end
# good
class A
def test
puts 'hello'
end
end
----
[#allowedpatterns_-____s_module__-layoutindentationwidth]
==== AllowedPatterns: ['^\s*module']
[source,ruby]
----
# bad
module A
class B
def test
puts 'hello'
end
end
end
# good
module A
class B
def test
puts 'hello'
end
end
end
----
[#configurable-attributes-layoutindentationwidth]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| Width
| `2`
| Integer
| AllowedPatterns
| `[]`
| Array
|===
[#references-layoutindentationwidth]
=== References
* https://rubystyle.guide#spaces-indentation
[#layoutinitialindentation]
== Layout/InitialIndentation
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
Checks for indentation of the first non-blank non-comment
line in a file.
[#examples-layoutinitialindentation]
=== Examples
[source,ruby]
----
# bad
class A
def foo; end
end
# good
class A
def foo; end
end
----
[#layoutleadingcommentspace]
== Layout/LeadingCommentSpace
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| 0.73
|===
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.
[#examples-layoutleadingcommentspace]
=== Examples
[source,ruby]
----
# bad
#Some comment
# good
# Some comment
----
[#allowdoxygencommentstyle_-false-_default_-layoutleadingcommentspace]
==== AllowDoxygenCommentStyle: false (default)
[source,ruby]
----
# bad
#**
# Some comment
# Another line of comment
#*
----
[#allowdoxygencommentstyle_-true-layoutleadingcommentspace]
==== AllowDoxygenCommentStyle: true
[source,ruby]
----
# good
#**
# Some comment
# Another line of comment
#*
----
[#allowgemfilerubycomment_-false-_default_-layoutleadingcommentspace]
==== AllowGemfileRubyComment: false (default)
[source,ruby]
----
# bad
#ruby=2.7.0
#ruby-gemset=myproject
----
[#allowgemfilerubycomment_-true-layoutleadingcommentspace]
==== AllowGemfileRubyComment: true
[source,ruby]
----
# good
#ruby=2.7.0
#ruby-gemset=myproject
----
[#allowrbsinlineannotation_-false-_default_-layoutleadingcommentspace]
==== AllowRBSInlineAnnotation: false (default)
[source,ruby]
----
# bad
include Enumerable #[Integer]
attr_reader :name #: String
attr_reader :age #: Integer?
----
[#allowrbsinlineannotation_-true-layoutleadingcommentspace]
==== AllowRBSInlineAnnotation: true
[source,ruby]
----
# good
include Enumerable #[Integer]
attr_reader :name #: String
attr_reader :age #: Integer?
----
[#allowsteepannotation_-false-_default_-layoutleadingcommentspace]
==== AllowSteepAnnotation: false (default)
[source,ruby]
----
# bad
[1, 2, 3].each_with_object([]) do |n, list| #$ Array[Integer]
list << n
end
name = 'John' #: String
----
[#allowsteepannotation_-true-layoutleadingcommentspace]
==== AllowSteepAnnotation: true
[source,ruby]
----
# good
[1, 2, 3].each_with_object([]) do |n, list| #$ Array[Integer]
list << n
end
name = 'John' #: String
----
[#configurable-attributes-layoutleadingcommentspace]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| AllowDoxygenCommentStyle
| `false`
| Boolean
| AllowGemfileRubyComment
| `false`
| Boolean
| AllowRBSInlineAnnotation
| `false`
| Boolean
| AllowSteepAnnotation
| `false`
| Boolean
|===
[#references-layoutleadingcommentspace]
=== References
* https://rubystyle.guide#hash-space
[#layoutleadingemptylines]
== Layout/LeadingEmptyLines
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.57
| 0.77
|===
Checks for unnecessary leading blank lines at the beginning
of a file.
[#examples-layoutleadingemptylines]
=== Examples
[source,ruby]
----
# bad
# (start of file)
class Foo
end
# bad
# (start of file)
# a comment
# good
# (start of file)
class Foo
end
# good
# (start of file)
# a comment
----
[#layoutlinecontinuationleadingspace]
== Layout/LineContinuationLeadingSpace
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Pending
| Yes
| Always
| 1.31
| 1.45
|===
Checks that strings broken over multiple lines (by a backslash) contain
trailing spaces instead of leading spaces (default) or leading spaces
instead of trailing spaces.
[#examples-layoutlinecontinuationleadingspace]
=== Examples
[#enforcedstyle_-trailing-_default_-layoutlinecontinuationleadingspace]
==== EnforcedStyle: trailing (default)
[source,ruby]
----
# bad
'this text contains a lot of' \
' spaces'
# good
'this text contains a lot of ' \
'spaces'
# bad
'this text is too' \
' long'
# good
'this text is too ' \
'long'
----
[#enforcedstyle_-leading-layoutlinecontinuationleadingspace]
==== EnforcedStyle: leading
[source,ruby]
----
# bad
'this text contains a lot of ' \
'spaces'
# good
'this text contains a lot of' \
' spaces'
# bad
'this text is too ' \
'long'
# good
'this text is too' \
' long'
----
[#configurable-attributes-layoutlinecontinuationleadingspace]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `trailing`
| `leading`, `trailing`
|===
[#layoutlinecontinuationspacing]
== Layout/LineContinuationSpacing
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Pending
| Yes
| Always
| 1.31
| -
|===
Checks that the backslash of a line continuation is separated from
preceding text by exactly one space (default) or zero spaces.
[#examples-layoutlinecontinuationspacing]
=== Examples
[#enforcedstyle_-space-_default_-layoutlinecontinuationspacing]
==== EnforcedStyle: space (default)
[source,ruby]
----
# bad
'a'\
'b' \
'c'
# good
'a' \
'b' \
'c'
----
[#enforcedstyle_-no_space-layoutlinecontinuationspacing]
==== EnforcedStyle: no_space
[source,ruby]
----
# bad
'a' \
'b' \
'c'
# good
'a'\
'b'\
'c'
----
[#configurable-attributes-layoutlinecontinuationspacing]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `space`
| `space`, `no_space`
|===
[#layoutlineendstringconcatenationindentation]
== Layout/LineEndStringConcatenationIndentation
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Pending
| Yes
| Always
| 1.18
| -
|===
Checks the indentation of the next line after a line that ends with a string
literal and a backslash.
If `EnforcedStyle: aligned` is set, the concatenated string parts shall be aligned with the
first part. There are some exceptions, such as implicit return values, where the
concatenated string parts shall be indented regardless of `EnforcedStyle` configuration.
If `EnforcedStyle: indented` is set, it's the second line that shall be indented one step
more than the first line. Lines 3 and forward shall be aligned with line 2.
[#examples-layoutlineendstringconcatenationindentation]
=== Examples
[source,ruby]
----
# bad
def some_method
'x' \
'y' \
'z'
end
my_hash = {
first: 'a message' \
'in two parts'
}
# good
def some_method
'x' \
'y' \
'z'
end
----
[#enforcedstyle_-aligned-_default_-layoutlineendstringconcatenationindentation]
==== EnforcedStyle: aligned (default)
[source,ruby]
----
# bad
puts 'x' \
'y'
my_hash = {
first: 'a message' \
'in two parts'
}
# good
puts 'x' \
'y'
my_hash = {
first: 'a message' \
'in two parts'
}
----
[#enforcedstyle_-indented-layoutlineendstringconcatenationindentation]
==== EnforcedStyle: indented
[source,ruby]
----
# bad
result = 'x' \
'y'
my_hash = {
first: 'a message' \
'in two parts'
}
# good
result = 'x' \
'y'
my_hash = {
first: 'a message' \
'in two parts'
}
----
[#configurable-attributes-layoutlineendstringconcatenationindentation]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `aligned`
| `aligned`, `indented`
| IndentationWidth
| `<none>`
| Integer
|===
[#layoutlinelength]
== Layout/LineLength
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.25
| 1.4
|===
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 cops
are recommended to further format the broken lines.
(Many of these are enabled by default.)
* `Layout/ArgumentAlignment`
* `Layout/ArrayAlignment`
* `Layout/BlockAlignment`
* `Layout/BlockEndNewline`
* `Layout/ClosingParenthesisIndentation`
* `Layout/FirstArgumentIndentation`
* `Layout/FirstArrayElementIndentation`
* `Layout/FirstHashElementIndentation`
* `Layout/FirstParameterIndentation`
* `Layout/HashAlignment`
* `Layout/IndentationWidth`
* `Layout/MultilineArrayLineBreaks`
* `Layout/MultilineBlockLayout`
* `Layout/MultilineHashBraceLayout`
* `Layout/MultilineHashKeyLineBreaks`
* `Layout/MultilineMethodArgumentLineBreaks`
* `Layout/MultilineMethodParameterLineBreaks`
* `Layout//ParameterAlignment`
* `Style/BlockDelimiters`
Together, these cops will pretty print hashes, arrays,
method calls, etc. For example, let's say the max columns
is 25:
[#examples-layoutlinelength]
=== Examples
[source,ruby]
----
# 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-layoutlinelength]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| Max
| `120`
| Integer
| AllowHeredoc
| `true`
| Boolean
| AllowURI
| `true`
| Boolean
| URISchemes
| `http`, `https`
| Array
| IgnoreCopDirectives
| `true`
| Boolean
| AllowedPatterns
| `[]`
| Array
|===
[#references-layoutlinelength]
=== References
* https://rubystyle.guide#max-line-length
[#layoutmultilinearraybracelayout]
== Layout/MultilineArrayBraceLayout
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
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.
[#examples-layoutmultilinearraybracelayout]
=== Examples
[#enforcedstyle_-symmetrical-_default_-layoutmultilinearraybracelayout]
==== EnforcedStyle: symmetrical (default)
[source,ruby]
----
# bad
[ :a,
:b
]
# bad
[
:a,
:b ]
# good
[ :a,
:b ]
# good
[
:a,
:b
]
----
[#enforcedstyle_-new_line-layoutmultilinearraybracelayout]
==== EnforcedStyle: new_line
[source,ruby]
----
# bad
[
:a,
:b ]
# bad
[ :a,
:b ]
# good
[ :a,
:b
]
# good
[
:a,
:b
]
----
[#enforcedstyle_-same_line-layoutmultilinearraybracelayout]
==== EnforcedStyle: same_line
[source,ruby]
----
# bad
[ :a,
:b
]
# bad
[
:a,
:b
]
# good
[
:a,
:b ]
# good
[ :a,
:b ]
----
[#configurable-attributes-layoutmultilinearraybracelayout]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `symmetrical`
| `symmetrical`, `new_line`, `same_line`
|===
[#layoutmultilinearraylinebreaks]
== Layout/MultilineArrayLineBreaks
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Disabled
| Yes
| Always
| 0.67
| -
|===
Ensures that each item in a multi-line array
starts on a separate line.
[#examples-layoutmultilinearraylinebreaks]
=== Examples
[source,ruby]
----
# bad
[
a, b,
c
]
# good
[
a,
b,
c
]
# good
[
a,
b,
foo(
bar
)
]
----
[#allowmultilinefinalelement_-false-_default_-layoutmultilinearraylinebreaks]
==== AllowMultilineFinalElement: false (default)
[source,ruby]
----
# bad
[a, b, foo(
bar
)]
----
[#allowmultilinefinalelement_-true-layoutmultilinearraylinebreaks]
==== AllowMultilineFinalElement: true
[source,ruby]
----
# good
[a, b, foo(
bar
)]
----
[#configurable-attributes-layoutmultilinearraylinebreaks]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| xref:cops_layout.adoc#allowmultilinefinalelement[AllowMultilineFinalElement]
| `false`
| Boolean
|===
[#layoutmultilineassignmentlayout]
== Layout/MultilineAssignmentLayout
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Disabled
| Yes
| Always
| 0.49
| -
|===
Checks whether the multiline assignments have a newline
after the assignment operator.
[#examples-layoutmultilineassignmentlayout]
=== Examples
[#enforcedstyle_-new_line-_default_-layoutmultilineassignmentlayout]
==== EnforcedStyle: new_line (default)
[source,ruby]
----
# bad
foo = if expression
'bar'
end
# good
foo =
if expression
'bar'
end
# good
foo =
begin
compute
rescue => e
nil
end
----
[#enforcedstyle_-same_line-layoutmultilineassignmentlayout]
==== EnforcedStyle: same_line
[source,ruby]
----
# good
foo = if expression
'bar'
end
----
[#supportedtypes_-__block__-_case__-_class__-_if__-_kwbegin__-_module__-_default_-layoutmultilineassignmentlayout]
==== SupportedTypes: ['block', 'case', 'class', 'if', 'kwbegin', 'module'] (default)
[source,ruby]
----
# good
foo =
if expression
'bar'
end
# good
foo =
[1].map do |i|
i + 1
end
----
[#supportedtypes_-__block__-layoutmultilineassignmentlayout]
==== SupportedTypes: ['block']
[source,ruby]
----
# good
foo = if expression
'bar'
end
# good
foo =
[1].map do |i|
'bar' * i
end
----
[#configurable-attributes-layoutmultilineassignmentlayout]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `new_line`
| `same_line`, `new_line`
|===
[#references-layoutmultilineassignmentlayout]
=== References
* https://rubystyle.guide#indent-conditional-assignment
[#layoutmultilineblocklayout]
== Layout/MultilineBlockLayout
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
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.
[#examples-layoutmultilineblocklayout]
=== Examples
[source,ruby]
----
# bad
blah do |i| foo(i)
bar(i)
end
# bad
blah do
|i| foo(i)
bar(i)
end
# good
blah do |i|
foo(i)
bar(i)
end
# bad
blah { |i| foo(i)
bar(i)
}
# good
blah { |i|
foo(i)
bar(i)
}
# good
blah { |
long_list,
of_parameters,
that_would_not,
fit_on_one_line
|
foo(i)
bar(i)
}
----
[#layoutmultilinehashbracelayout]
== Layout/MultilineHashBraceLayout
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
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.
[#examples-layoutmultilinehashbracelayout]
=== Examples
[#enforcedstyle_-symmetrical-_default_-layoutmultilinehashbracelayout]
==== EnforcedStyle: symmetrical (default)
[source,ruby]
----
# bad
{ a: 1,
b: 2
}
# bad
{
a: 1,
b: 2 }
# good
{ a: 1,
b: 2 }
# good
{
a: 1,
b: 2
}
----
[#enforcedstyle_-new_line-layoutmultilinehashbracelayout]
==== EnforcedStyle: new_line
[source,ruby]
----
# bad
{
a: 1,
b: 2 }
# bad
{ a: 1,
b: 2 }
# good
{ a: 1,
b: 2
}
# good
{
a: 1,
b: 2
}
----
[#enforcedstyle_-same_line-layoutmultilinehashbracelayout]
==== EnforcedStyle: same_line
[source,ruby]
----
# bad
{ a: 1,
b: 2
}
# bad
{
a: 1,
b: 2
}
# good
{
a: 1,
b: 2 }
# good
{ a: 1,
b: 2 }
----
[#configurable-attributes-layoutmultilinehashbracelayout]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `symmetrical`
| `symmetrical`, `new_line`, `same_line`
|===
[#layoutmultilinehashkeylinebreaks]
== Layout/MultilineHashKeyLineBreaks
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Disabled
| Yes
| Always
| 0.67
| -
|===
Ensures that each key in a multi-line hash
starts on a separate line.
[#examples-layoutmultilinehashkeylinebreaks]
=== Examples
[source,ruby]
----
# bad
{
a: 1, b: 2,
c: 3
}
# good
{
a: 1,
b: 2,
c: 3
}
# good
{
a: 1,
b: {
c: 3,
}
}
----
[#allowmultilinefinalelement_-false-_default_-layoutmultilinehashkeylinebreaks]
==== AllowMultilineFinalElement: false (default)
[source,ruby]
----
# bad
{ a: 1, b: {
c: 3,
}}
----
[#allowmultilinefinalelement_-true-layoutmultilinehashkeylinebreaks]
==== AllowMultilineFinalElement: true
[source,ruby]
----
# good
{ a: 1, b: {
c: 3,
}}
----
[#configurable-attributes-layoutmultilinehashkeylinebreaks]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| xref:cops_layout.adoc#allowmultilinefinalelement[AllowMultilineFinalElement]
| `false`
| Boolean
|===
[#layoutmultilinemethodargumentlinebreaks]
== Layout/MultilineMethodArgumentLineBreaks
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Disabled
| Yes
| Always
| 0.67
| -
|===
Ensures that each argument in a multi-line method call
starts on a separate line.
NOTE: This cop does not move the first argument, if you want that to
be on a separate line, see `Layout/FirstMethodArgumentLineBreak`.
[#examples-layoutmultilinemethodargumentlinebreaks]
=== Examples
[source,ruby]
----
# bad
foo(a, b,
c
)
# bad
foo(a, b, {
foo: "bar",
})
# good
foo(
a,
b,
c
)
# good
foo(a, b, c)
----
[#allowmultilinefinalelement_-false-_default_-layoutmultilinemethodargumentlinebreaks]
==== AllowMultilineFinalElement: false (default)
[source,ruby]
----
# good
foo(
a,
b,
{
foo: "bar",
}
)
----
[#allowmultilinefinalelement_-true-layoutmultilinemethodargumentlinebreaks]
==== AllowMultilineFinalElement: true
[source,ruby]
----
# good
foo(
a,
b,
{
foo: "bar",
}
)
----
[#configurable-attributes-layoutmultilinemethodargumentlinebreaks]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| xref:cops_layout.adoc#allowmultilinefinalelement[AllowMultilineFinalElement]
| `false`
| Boolean
|===
[#layoutmultilinemethodcallbracelayout]
== Layout/MultilineMethodCallBraceLayout
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
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.
[#examples-layoutmultilinemethodcallbracelayout]
=== Examples
[#enforcedstyle_-symmetrical-_default_-layoutmultilinemethodcallbracelayout]
==== EnforcedStyle: symmetrical (default)
[source,ruby]
----
# bad
foo(a,
b
)
# bad
foo(
a,
b)
# good
foo(a,
b)
# good
foo(
a,
b
)
----
[#enforcedstyle_-new_line-layoutmultilinemethodcallbracelayout]
==== EnforcedStyle: new_line
[source,ruby]
----
# bad
foo(
a,
b)
# bad
foo(a,
b)
# good
foo(a,
b
)
# good
foo(
a,
b
)
----
[#enforcedstyle_-same_line-layoutmultilinemethodcallbracelayout]
==== EnforcedStyle: same_line
[source,ruby]
----
# bad
foo(a,
b
)
# bad
foo(
a,
b
)
# good
foo(
a,
b)
# good
foo(a,
b)
----
[#configurable-attributes-layoutmultilinemethodcallbracelayout]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `symmetrical`
| `symmetrical`, `new_line`, `same_line`
|===
[#layoutmultilinemethodcallindentation]
== Layout/MultilineMethodCallIndentation
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
Checks the indentation of the method name part in method calls
that span more than one line.
[#examples-layoutmultilinemethodcallindentation]
=== Examples
[#enforcedstyle_-aligned-_default_-layoutmultilinemethodcallindentation]
==== EnforcedStyle: aligned (default)
[source,ruby]
----
# bad
while myvariable
.b
# do something
end
# good
while myvariable
.b
# do something
end
# good
Thing.a
.b
.c
----
[#enforcedstyle_-indented-layoutmultilinemethodcallindentation]
==== EnforcedStyle: indented
[source,ruby]
----
# good
while myvariable
.b
# do something
end
----
[#enforcedstyle_-indented_relative_to_receiver-layoutmultilinemethodcallindentation]
==== EnforcedStyle: indented_relative_to_receiver
[source,ruby]
----
# good
while myvariable
.a
.b
# do something
end
# good
myvariable = Thing
.a
.b
.c
----
[#configurable-attributes-layoutmultilinemethodcallindentation]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `aligned`
| `aligned`, `indented`, `indented_relative_to_receiver`
| IndentationWidth
| `<none>`
| Integer
|===
[#layoutmultilinemethoddefinitionbracelayout]
== Layout/MultilineMethodDefinitionBraceLayout
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
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.
[#examples-layoutmultilinemethoddefinitionbracelayout]
=== Examples
[#enforcedstyle_-symmetrical-_default_-layoutmultilinemethoddefinitionbracelayout]
==== EnforcedStyle: symmetrical (default)
[source,ruby]
----
# bad
def foo(a,
b
)
end
# bad
def foo(
a,
b)
end
# good
def foo(a,
b)
end
# good
def foo(
a,
b
)
end
----
[#enforcedstyle_-new_line-layoutmultilinemethoddefinitionbracelayout]
==== EnforcedStyle: new_line
[source,ruby]
----
# bad
def foo(
a,
b)
end
# bad
def foo(a,
b)
end
# good
def foo(a,
b
)
end
# good
def foo(
a,
b
)
end
----
[#enforcedstyle_-same_line-layoutmultilinemethoddefinitionbracelayout]
==== EnforcedStyle: same_line
[source,ruby]
----
# bad
def foo(a,
b
)
end
# bad
def foo(
a,
b
)
end
# good
def foo(
a,
b)
end
# good
def foo(a,
b)
end
----
[#configurable-attributes-layoutmultilinemethoddefinitionbracelayout]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `symmetrical`
| `symmetrical`, `new_line`, `same_line`
|===
[#layoutmultilinemethodparameterlinebreaks]
== Layout/MultilineMethodParameterLineBreaks
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Disabled
| Yes
| Always
| 1.32
| -
|===
Ensures that each parameter in a multi-line method definition
starts on a separate line.
NOTE: This cop does not move the first argument, if you want that to
be on a separate line, see `Layout/FirstMethodParameterLineBreak`.
[#examples-layoutmultilinemethodparameterlinebreaks]
=== Examples
[source,ruby]
----
# bad
def foo(a, b,
c
)
end
# good
def foo(
a,
b,
c
)
end
# good
def foo(
a,
b = {
foo: "bar",
}
)
end
# good
def foo(a, b, c)
end
----
[#allowmultilinefinalelement_-false-_default_-layoutmultilinemethodparameterlinebreaks]
==== AllowMultilineFinalElement: false (default)
[source,ruby]
----
# bad
def foo(a, b = {
foo: "bar",
})
end
----
[#allowmultilinefinalelement_-true-layoutmultilinemethodparameterlinebreaks]
==== AllowMultilineFinalElement: true
[source,ruby]
----
# good
def foo(a, b = {
foo: "bar",
})
end
----
[#configurable-attributes-layoutmultilinemethodparameterlinebreaks]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| xref:cops_layout.adoc#allowmultilinefinalelement[AllowMultilineFinalElement]
| `false`
| Boolean
|===
[#layoutmultilineoperationindentation]
== Layout/MultilineOperationIndentation
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
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, an explicit `return` statement, etc. In other contexts, the second operand should
be indented regardless of enforced style.
[#examples-layoutmultilineoperationindentation]
=== Examples
[#enforcedstyle_-aligned-_default_-layoutmultilineoperationindentation]
==== EnforcedStyle: aligned (default)
[source,ruby]
----
# bad
if a +
b
something &&
something_else
end
# good
if a +
b
something &&
something_else
end
----
[#enforcedstyle_-indented-layoutmultilineoperationindentation]
==== EnforcedStyle: indented
[source,ruby]
----
# bad
if a +
b
something &&
something_else
end
# good
if a +
b
something &&
something_else
end
----
[#configurable-attributes-layoutmultilineoperationindentation]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `aligned`
| `aligned`, `indented`
| IndentationWidth
| `<none>`
| Integer
|===
[#layoutparameteralignment]
== Layout/ParameterAlignment
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 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-layoutparameteralignment]
=== Examples
[#enforcedstyle_-with_first_parameter-_default_-layoutparameteralignment]
==== EnforcedStyle: with_first_parameter (default)
[source,ruby]
----
# good
def foo(bar,
baz)
123
end
def foo(
bar,
baz
)
123
end
# bad
def foo(bar,
baz)
123
end
# bad
def foo(
bar,
baz)
123
end
----
[#enforcedstyle_-with_fixed_indentation-layoutparameteralignment]
==== EnforcedStyle: with_fixed_indentation
[source,ruby]
----
# good
def foo(bar,
baz)
123
end
def foo(
bar,
baz
)
123
end
# bad
def foo(bar,
baz)
123
end
# bad
def foo(
bar,
baz)
123
end
----
[#configurable-attributes-layoutparameteralignment]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `with_first_parameter`
| `with_first_parameter`, `with_fixed_indentation`
| IndentationWidth
| `<none>`
| Integer
|===
[#references-layoutparameteralignment]
=== References
* https://rubystyle.guide#no-double-indent
[#layoutredundantlinebreak]
== Layout/RedundantLineBreak
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Disabled
| Yes
| Always
| 1.13
| -
|===
Checks whether certain expressions, e.g. method calls, that could fit
completely on a single line, are broken up into multiple lines unnecessarily.
[#examples-layoutredundantlinebreak]
=== Examples
[#any-configuration-layoutredundantlinebreak]
==== any configuration
[source,ruby]
----
# bad
foo(
a,
b
)
puts 'string that fits on ' \
'a single line'
things
.select { |thing| thing.cond? }
.join('-')
# good
foo(a, b)
puts 'string that fits on a single line'
things.select { |thing| thing.cond? }.join('-')
----
[#inspectblocks_-false-_default_-layoutredundantlinebreak]
==== InspectBlocks: false (default)
[source,ruby]
----
# good
foo(a) do |x|
puts x
end
----
[#inspectblocks_-true-layoutredundantlinebreak]
==== InspectBlocks: true
[source,ruby]
----
# bad
foo(a) do |x|
puts x
end
# good
foo(a) { |x| puts x }
----
[#configurable-attributes-layoutredundantlinebreak]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| InspectBlocks
| `false`
| Boolean
|===
[#layoutrescueensurealignment]
== Layout/RescueEnsureAlignment
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
Checks whether the rescue and ensure keywords are aligned
properly.
[#examples-layoutrescueensurealignment]
=== Examples
[source,ruby]
----
# bad
begin
something
rescue
puts 'error'
end
# good
begin
something
rescue
puts 'error'
end
----
[#layoutsinglelineblockchain]
== Layout/SingleLineBlockChain
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Disabled
| Yes
| Always
| 1.14
| -
|===
Checks if method calls are chained onto single line blocks. It considers that a
line break before the dot improves the readability of the code.
[#examples-layoutsinglelineblockchain]
=== Examples
[source,ruby]
----
# bad
example.select { |item| item.cond? }.join('-')
# good
example.select { |item| item.cond? }
.join('-')
# good (not a concern for this cop)
example.select do |item|
item.cond?
end.join('-')
----
[#layoutspaceaftercolon]
== Layout/SpaceAfterColon
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
Checks for colon (:) not followed by some kind of space.
N.B. this cop does not handle spaces after a ternary operator, which are
instead handled by Layout/SpaceAroundOperators.
[#examples-layoutspaceaftercolon]
=== Examples
[source,ruby]
----
# bad
def f(a:, b:2); {a:3}; end
# good
def f(a:, b: 2); {a: 3}; end
----
[#references-layoutspaceaftercolon]
=== References
* https://rubystyle.guide#spaces-operators
[#layoutspaceaftercomma]
== Layout/SpaceAfterComma
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
Checks for comma (,) not followed by some kind of space.
[#examples-layoutspaceaftercomma]
=== Examples
[source,ruby]
----
# bad
[1,2]
{ foo:bar,}
# good
[1, 2]
{ foo:bar, }
----
[#references-layoutspaceaftercomma]
=== References
* https://rubystyle.guide#spaces-operators
[#layoutspaceaftermethodname]
== Layout/SpaceAfterMethodName
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
Checks for space between a method name and a left parenthesis in defs.
[#examples-layoutspaceaftermethodname]
=== Examples
[source,ruby]
----
# bad
def func (x) end
def method= (y) end
# good
def func(x) end
def method=(y) end
----
[#references-layoutspaceaftermethodname]
=== References
* https://rubystyle.guide#parens-no-spaces
[#layoutspaceafternot]
== Layout/SpaceAfterNot
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
Checks for space after `!`.
[#examples-layoutspaceafternot]
=== Examples
[source,ruby]
----
# bad
! something
# good
!something
----
[#references-layoutspaceafternot]
=== References
* https://rubystyle.guide#no-space-bang
[#layoutspaceaftersemicolon]
== Layout/SpaceAfterSemicolon
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
Checks for semicolon (;) not followed by some kind of space.
[#examples-layoutspaceaftersemicolon]
=== Examples
[source,ruby]
----
# bad
x = 1;y = 2
# good
x = 1; y = 2
----
[#references-layoutspaceaftersemicolon]
=== References
* https://rubystyle.guide#spaces-operators
[#layoutspacearoundblockparameters]
== Layout/SpaceAroundBlockParameters
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 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.
[#examples-layoutspacearoundblockparameters]
=== Examples
[#enforcedstyleinsidepipes_-no_space-_default_-layoutspacearoundblockparameters]
==== EnforcedStyleInsidePipes: no_space (default)
[source,ruby]
----
# bad
{}.each { | x, y |puts x }
->( x, y ) { puts x }
# good
{}.each { |x, y| puts x }
->(x, y) { puts x }
----
[#enforcedstyleinsidepipes_-space-layoutspacearoundblockparameters]
==== EnforcedStyleInsidePipes: space
[source,ruby]
----
# bad
{}.each { |x, y| puts x }
->(x, y) { puts x }
# good
{}.each { | x, y | puts x }
->( x, y ) { puts x }
----
[#configurable-attributes-layoutspacearoundblockparameters]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyleInsidePipes
| `no_space`
| `space`, `no_space`
|===
[#layoutspacearoundequalsinparameterdefault]
== Layout/SpaceAroundEqualsInParameterDefault
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
Checks that the equals signs in parameter default assignments
have or don't have surrounding space depending on configuration.
[#examples-layoutspacearoundequalsinparameterdefault]
=== Examples
[#enforcedstyle_-space-_default_-layoutspacearoundequalsinparameterdefault]
==== EnforcedStyle: space (default)
[source,ruby]
----
# bad
def some_method(arg1=:default, arg2=nil, arg3=[])
# do something...
end
# good
def some_method(arg1 = :default, arg2 = nil, arg3 = [])
# do something...
end
----
[#enforcedstyle_-no_space-layoutspacearoundequalsinparameterdefault]
==== EnforcedStyle: no_space
[source,ruby]
----
# bad
def some_method(arg1 = :default, arg2 = nil, arg3 = [])
# do something...
end
# good
def some_method(arg1=:default, arg2=nil, arg3=[])
# do something...
end
----
[#configurable-attributes-layoutspacearoundequalsinparameterdefault]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `space`
| `space`, `no_space`
|===
[#references-layoutspacearoundequalsinparameterdefault]
=== References
* https://rubystyle.guide#spaces-around-equals
[#layoutspacearoundkeyword]
== Layout/SpaceAroundKeyword
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
Checks the spacing around the keywords.
[#examples-layoutspacearoundkeyword]
=== Examples
[source,ruby]
----
# bad
something 'test'do|x|
end
while(something)
end
something = 123if test
# good
something 'test' do |x|
end
while (something)
end
something = 123 if test
----
[#layoutspacearoundmethodcalloperator]
== Layout/SpaceAroundMethodCallOperator
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.82
| -
|===
Checks method call operators to not have spaces around them.
[#examples-layoutspacearoundmethodcalloperator]
=== Examples
[source,ruby]
----
# bad
foo. bar
foo .bar
foo . bar
foo. bar .buzz
foo
. bar
. buzz
foo&. bar
foo &.bar
foo &. bar
foo &. bar&. buzz
RuboCop:: Cop
RuboCop:: Cop:: Base
:: RuboCop::Cop
# good
foo.bar
foo.bar.buzz
foo
.bar
.buzz
foo&.bar
foo&.bar&.buzz
RuboCop::Cop
RuboCop::Cop::Base
::RuboCop::Cop
----
[#layoutspacearoundoperators]
== Layout/SpaceAroundOperators
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 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-layoutspacearoundoperators]
=== Examples
[source,ruby]
----
# bad
total = 3*4
"apple"+"juice"
my_number = 38/4
# good
total = 3 * 4
"apple" + "juice"
my_number = 38 / 4
----
[#allowforalignment_-true-_default_-layoutspacearoundoperators]
==== AllowForAlignment: true (default)
[source,ruby]
----
# good
{
1 => 2,
11 => 3
}
----
[#allowforalignment_-false-layoutspacearoundoperators]
==== AllowForAlignment: false
[source,ruby]
----
# bad
{
1 => 2,
11 => 3
}
----
[#enforcedstyleforexponentoperator_-no_space-_default_-layoutspacearoundoperators]
==== EnforcedStyleForExponentOperator: no_space (default)
[source,ruby]
----
# bad
a ** b
# good
a**b
----
[#enforcedstyleforexponentoperator_-space-layoutspacearoundoperators]
==== EnforcedStyleForExponentOperator: space
[source,ruby]
----
# bad
a**b
# good
a ** b
----
[#enforcedstyleforrationalliterals_-no_space-_default_-layoutspacearoundoperators]
==== EnforcedStyleForRationalLiterals: no_space (default)
[source,ruby]
----
# bad
1 / 48r
# good
1/48r
----
[#enforcedstyleforrationalliterals_-space-layoutspacearoundoperators]
==== EnforcedStyleForRationalLiterals: space
[source,ruby]
----
# bad
1/48r
# good
1 / 48r
----
[#configurable-attributes-layoutspacearoundoperators]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| AllowForAlignment
| `true`
| Boolean
| EnforcedStyleForExponentOperator
| `no_space`
| `space`, `no_space`
| EnforcedStyleForRationalLiterals
| `no_space`
| `space`, `no_space`
|===
[#references-layoutspacearoundoperators]
=== References
* https://rubystyle.guide#spaces-operators
[#layoutspacebeforeblockbraces]
== Layout/SpaceBeforeBlockBraces
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| 0.52
|===
Checks that block braces have or don't have a space before the opening
brace depending on configuration.
[#examples-layoutspacebeforeblockbraces]
=== Examples
[#enforcedstyle_-space-_default_-layoutspacebeforeblockbraces]
==== EnforcedStyle: space (default)
[source,ruby]
----
# bad
foo.map{ |a|
a.bar.to_s
}
# good
foo.map { |a|
a.bar.to_s
}
----
[#enforcedstyle_-no_space-layoutspacebeforeblockbraces]
==== EnforcedStyle: no_space
[source,ruby]
----
# bad
foo.map { |a|
a.bar.to_s
}
# good
foo.map{ |a|
a.bar.to_s
}
----
[#enforcedstyleforemptybraces_-space-_default_-layoutspacebeforeblockbraces]
==== EnforcedStyleForEmptyBraces: space (default)
[source,ruby]
----
# bad
7.times{}
# good
7.times {}
----
[#enforcedstyleforemptybraces_-no_space-layoutspacebeforeblockbraces]
==== EnforcedStyleForEmptyBraces: no_space
[source,ruby]
----
# bad
7.times {}
# good
7.times{}
----
[#configurable-attributes-layoutspacebeforeblockbraces]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `space`
| `space`, `no_space`
| EnforcedStyleForEmptyBraces
| `space`
| `space`, `no_space`
|===
[#layoutspacebeforebrackets]
== Layout/SpaceBeforeBrackets
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Pending
| Yes
| Always
| 1.7
| -
|===
Checks for space between the name of a receiver and a left
brackets.
[#examples-layoutspacebeforebrackets]
=== Examples
[source,ruby]
----
# bad
collection [index_or_key]
# good
collection[index_or_key]
----
[#references-layoutspacebeforebrackets]
=== References
* https://rubystyle.guide#space-in-brackets-access
[#layoutspacebeforecomma]
== Layout/SpaceBeforeComma
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
Checks for comma (,) preceded by space.
[#examples-layoutspacebeforecomma]
=== Examples
[source,ruby]
----
# bad
[1 , 2 , 3]
a(1 , 2)
each { |a , b| }
# good
[1, 2, 3]
a(1, 2)
each { |a, b| }
----
[#layoutspacebeforecomment]
== Layout/SpaceBeforeComment
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
Checks for missing space between a token and a comment on the
same line.
[#examples-layoutspacebeforecomment]
=== Examples
[source,ruby]
----
# bad
1 + 1# this operation does ...
# good
1 + 1 # this operation does ...
----
[#layoutspacebeforefirstarg]
== Layout/SpaceBeforeFirstArg
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 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.
[#examples-layoutspacebeforefirstarg]
=== Examples
[source,ruby]
----
# bad
something x
something y, z
something'hello'
# good
something x
something y, z
something 'hello'
----
[#configurable-attributes-layoutspacebeforefirstarg]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| AllowForAlignment
| `true`
| Boolean
|===
[#layoutspacebeforesemicolon]
== Layout/SpaceBeforeSemicolon
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
Checks for semicolon (;) preceded by space.
[#examples-layoutspacebeforesemicolon]
=== Examples
[source,ruby]
----
# bad
x = 1 ; y = 2
# good
x = 1; y = 2
----
[#layoutspaceinlambdaliteral]
== Layout/SpaceInLambdaLiteral
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
Checks for spaces between `->` and opening parameter
parenthesis (`(`) in lambda literals.
[#examples-layoutspaceinlambdaliteral]
=== Examples
[#enforcedstyle_-require_no_space-_default_-layoutspaceinlambdaliteral]
==== EnforcedStyle: require_no_space (default)
[source,ruby]
----
# bad
a = -> (x, y) { x + y }
# good
a = ->(x, y) { x + y }
----
[#enforcedstyle_-require_space-layoutspaceinlambdaliteral]
==== EnforcedStyle: require_space
[source,ruby]
----
# bad
a = ->(x, y) { x + y }
# good
a = -> (x, y) { x + y }
----
[#configurable-attributes-layoutspaceinlambdaliteral]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `require_no_space`
| `require_no_space`, `require_space`
|===
[#layoutspaceinsidearrayliteralbrackets]
== Layout/SpaceInsideArrayLiteralBrackets
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.52
| -
|===
Checks that brackets used for array literals have or don't have
surrounding space depending on configuration.
[#examples-layoutspaceinsidearrayliteralbrackets]
=== Examples
[#enforcedstyle_-no_space-_default_-layoutspaceinsidearrayliteralbrackets]
==== EnforcedStyle: no_space (default)
[source,ruby]
----
# 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_-space-layoutspaceinsidearrayliteralbrackets]
==== EnforcedStyle: space
[source,ruby]
----
# The `space` style enforces that array literals have
# surrounding space.
# bad
array = [a, b, c, d]
# good
array = [ a, b, c, d ]
----
[#enforcedstyle_-compact-layoutspaceinsidearrayliteralbrackets]
==== EnforcedStyle: compact
[source,ruby]
----
# 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 ]]
----
[#enforcedstyleforemptybrackets_-no_space-_default_-layoutspaceinsidearrayliteralbrackets]
==== EnforcedStyleForEmptyBrackets: no_space (default)
[source,ruby]
----
# The `no_space` EnforcedStyleForEmptyBrackets style enforces that
# empty array brackets do not contain spaces.
# bad
foo = [ ]
bar = [ ]
# good
foo = []
bar = []
----
[#enforcedstyleforemptybrackets_-space-layoutspaceinsidearrayliteralbrackets]
==== EnforcedStyleForEmptyBrackets: space
[source,ruby]
----
# The `space` EnforcedStyleForEmptyBrackets style enforces that
# empty array brackets contain exactly one space.
# bad
foo = []
bar = [ ]
# good
foo = [ ]
bar = [ ]
----
[#configurable-attributes-layoutspaceinsidearrayliteralbrackets]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `no_space`
| `space`, `no_space`, `compact`
| EnforcedStyleForEmptyBrackets
| `no_space`
| `space`, `no_space`
|===
[#layoutspaceinsidearraypercentliteral]
== Layout/SpaceInsideArrayPercentLiteral
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
Checks for unnecessary additional spaces inside array percent literals
(i.e. %i/%w).
Note that blank percent literals (e.g. `%i( )`) are checked by
`Layout/SpaceInsidePercentLiteralDelimiters`.
[#examples-layoutspaceinsidearraypercentliteral]
=== Examples
[source,ruby]
----
# bad
%w(foo bar baz)
# good
%i(foo bar baz)
----
[#layoutspaceinsideblockbraces]
== Layout/SpaceInsideBlockBraces
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 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-layoutspaceinsideblockbraces]
=== Examples
[#enforcedstyle_-space-_default_-layoutspaceinsideblockbraces]
==== EnforcedStyle: space (default)
[source,ruby]
----
# 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-layoutspaceinsideblockbraces]
==== EnforcedStyle: no_space
[source,ruby]
----
# 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_-layoutspaceinsideblockbraces]
==== EnforcedStyleForEmptyBraces: no_space (default)
[source,ruby]
----
# 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-layoutspaceinsideblockbraces]
==== EnforcedStyleForEmptyBraces: space
[source,ruby]
----
# 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 { }
----
[#spacebeforeblockparameters_-true-_default_-layoutspaceinsideblockbraces]
==== SpaceBeforeBlockParameters: true (default)
[source,ruby]
----
# The SpaceBeforeBlockParameters style set to `true` enforces that
# there is a space between `{` and `|`. Overrides `EnforcedStyle`
# if there is a conflict.
# bad
[1, 2, 3].each {|n| n * 2 }
# good
[1, 2, 3].each { |n| n * 2 }
----
[#spacebeforeblockparameters_-false-layoutspaceinsideblockbraces]
==== SpaceBeforeBlockParameters: false
[source,ruby]
----
# The SpaceBeforeBlockParameters style set to `false` enforces that
# there is no space between `{` and `|`. Overrides `EnforcedStyle`
# if there is a conflict.
# bad
[1, 2, 3].each { |n| n * 2 }
# good
[1, 2, 3].each {|n| n * 2 }
----
[#configurable-attributes-layoutspaceinsideblockbraces]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `space`
| `space`, `no_space`
| EnforcedStyleForEmptyBraces
| `no_space`
| `space`, `no_space`
| SpaceBeforeBlockParameters
| `true`
| Boolean
|===
[#layoutspaceinsidehashliteralbraces]
== Layout/SpaceInsideHashLiteralBraces
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
Checks that braces used for hash literals have or don't have
surrounding space depending on configuration.
[#examples-layoutspaceinsidehashliteralbraces]
=== Examples
[#enforcedstyle_-space-_default_-layoutspaceinsidehashliteralbraces]
==== EnforcedStyle: space (default)
[source,ruby]
----
# 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-layoutspaceinsidehashliteralbraces]
==== EnforcedStyle: no_space
[source,ruby]
----
# 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-layoutspaceinsidehashliteralbraces]
==== EnforcedStyle: compact
[source,ruby]
----
# 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 }}}
----
[#enforcedstyleforemptybraces_-no_space-_default_-layoutspaceinsidehashliteralbraces]
==== EnforcedStyleForEmptyBraces: no_space (default)
[source,ruby]
----
# The `no_space` EnforcedStyleForEmptyBraces style enforces that
# empty hash braces do not contain spaces.
# bad
foo = { }
bar = { }
baz = {
}
# good
foo = {}
bar = {}
baz = {}
----
[#enforcedstyleforemptybraces_-space-layoutspaceinsidehashliteralbraces]
==== EnforcedStyleForEmptyBraces: space
[source,ruby]
----
# The `space` EnforcedStyleForEmptyBraces style enforces that
# empty hash braces contain space.
# bad
foo = {}
# good
foo = { }
foo = { }
foo = {
}
----
[#configurable-attributes-layoutspaceinsidehashliteralbraces]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `space`
| `space`, `no_space`, `compact`
| EnforcedStyleForEmptyBraces
| `no_space`
| `space`, `no_space`
|===
[#references-layoutspaceinsidehashliteralbraces]
=== References
* https://rubystyle.guide#spaces-braces
[#layoutspaceinsideparens]
== Layout/SpaceInsideParens
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| 1.22
|===
Checks for spaces inside ordinary round parentheses.
[#examples-layoutspaceinsideparens]
=== Examples
[#enforcedstyle_-no_space-_default_-layoutspaceinsideparens]
==== EnforcedStyle: no_space (default)
[source,ruby]
----
# The `no_space` style enforces that parentheses do not have spaces.
# bad
f( 3)
g = (a + 3 )
f( )
# good
f(3)
g = (a + 3)
f()
----
[#enforcedstyle_-space-layoutspaceinsideparens]
==== EnforcedStyle: space
[source,ruby]
----
# The `space` style enforces that parentheses have a space at the
# beginning and end.
# Note: Empty parentheses should not have spaces.
# bad
f(3)
g = (a + 3)
y( )
# good
f( 3 )
g = ( a + 3 )
y()
----
[#enforcedstyle_-compact-layoutspaceinsideparens]
==== EnforcedStyle: compact
[source,ruby]
----
# The `compact` style enforces that parentheses have a space at the
# beginning with the exception that successive parentheses are allowed.
# Note: Empty parentheses should not have spaces.
# bad
f(3)
g = (a + 3)
y( )
g( f( x ) )
g( f( x( 3 ) ), 5 )
g( ( ( 3 + 5 ) * f) ** x, 5 )
# good
f( 3 )
g = ( a + 3 )
y()
g( f( x ))
g( f( x( 3 )), 5 )
g((( 3 + 5 ) * f ) ** x, 5 )
----
[#configurable-attributes-layoutspaceinsideparens]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `no_space`
| `space`, `compact`, `no_space`
|===
[#references-layoutspaceinsideparens]
=== References
* https://rubystyle.guide#spaces-braces
[#layoutspaceinsidepercentliteraldelimiters]
== Layout/SpaceInsidePercentLiteralDelimiters
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
Checks for unnecessary additional spaces inside the delimiters of
%i/%w/%x literals.
[#examples-layoutspaceinsidepercentliteraldelimiters]
=== Examples
[source,ruby]
----
# bad
%i( foo bar baz )
# good
%i(foo bar baz)
# bad
%w( foo bar baz )
# good
%w(foo bar baz)
# bad
%x( ls -l )
# good
%x(ls -l)
# bad
%w( )
%w(
)
# good
%w()
----
[#layoutspaceinsiderangeliteral]
== Layout/SpaceInsideRangeLiteral
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
Checks for spaces inside range literals.
[#examples-layoutspaceinsiderangeliteral]
=== Examples
[source,ruby]
----
# bad
1 .. 3
# good
1..3
# bad
'a' .. 'z'
# good
'a'..'z'
----
[#references-layoutspaceinsiderangeliteral]
=== References
* https://rubystyle.guide#no-space-inside-range-literals
[#layoutspaceinsidereferencebrackets]
== Layout/SpaceInsideReferenceBrackets
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.52
| 0.53
|===
Checks that reference brackets have or don't have
surrounding space depending on configuration.
[#examples-layoutspaceinsidereferencebrackets]
=== Examples
[#enforcedstyle_-no_space-_default_-layoutspaceinsidereferencebrackets]
==== EnforcedStyle: no_space (default)
[source,ruby]
----
# The `no_space` style enforces that reference brackets have
# no surrounding space.
# bad
hash[ :key ]
array[ index ]
# good
hash[:key]
array[index]
----
[#enforcedstyle_-space-layoutspaceinsidereferencebrackets]
==== EnforcedStyle: space
[source,ruby]
----
# The `space` style enforces that reference brackets have
# surrounding space.
# bad
hash[:key]
array[index]
# good
hash[ :key ]
array[ index ]
----
[#enforcedstyleforemptybrackets_-no_space-_default_-layoutspaceinsidereferencebrackets]
==== EnforcedStyleForEmptyBrackets: no_space (default)
[source,ruby]
----
# The `no_space` EnforcedStyleForEmptyBrackets style enforces that
# empty reference brackets do not contain spaces.
# bad
foo[ ]
foo[ ]
foo[
]
# good
foo[]
----
[#enforcedstyleforemptybrackets_-space-layoutspaceinsidereferencebrackets]
==== EnforcedStyleForEmptyBrackets: space
[source,ruby]
----
# The `space` EnforcedStyleForEmptyBrackets style enforces that
# empty reference brackets contain exactly one space.
# bad
foo[]
foo[ ]
foo[
]
# good
foo[ ]
----
[#configurable-attributes-layoutspaceinsidereferencebrackets]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `no_space`
| `space`, `no_space`
| EnforcedStyleForEmptyBrackets
| `no_space`
| `space`, `no_space`
|===
[#layoutspaceinsidestringinterpolation]
== Layout/SpaceInsideStringInterpolation
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| -
|===
Checks for whitespace within string interpolations.
[#examples-layoutspaceinsidestringinterpolation]
=== Examples
[#enforcedstyle_-no_space-_default_-layoutspaceinsidestringinterpolation]
==== EnforcedStyle: no_space (default)
[source,ruby]
----
# bad
var = "This is the #{ space } example"
# good
var = "This is the #{no_space} example"
----
[#enforcedstyle_-space-layoutspaceinsidestringinterpolation]
==== EnforcedStyle: space
[source,ruby]
----
# bad
var = "This is the #{no_space} example"
# good
var = "This is the #{ space } example"
----
[#configurable-attributes-layoutspaceinsidestringinterpolation]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `no_space`
| `space`, `no_space`
|===
[#references-layoutspaceinsidestringinterpolation]
=== References
* https://rubystyle.guide#string-interpolation
[#layouttrailingemptylines]
== Layout/TrailingEmptyLines
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| 0.77
|===
Looks for trailing blank lines and a final newline in the
source code.
[#examples-layouttrailingemptylines]
=== Examples
[#enforcedstyle_-final_newline-_default_-layouttrailingemptylines]
==== EnforcedStyle: final_newline (default)
[source,ruby]
----
# `final_newline` looks for one newline at the end of files.
# bad
class Foo; end
# EOF
# bad
class Foo; end # EOF
# good
class Foo; end
# EOF
----
[#enforcedstyle_-final_blank_line-layouttrailingemptylines]
==== EnforcedStyle: final_blank_line
[source,ruby]
----
# `final_blank_line` looks for one blank line followed by a new line
# at the end of files.
# bad
class Foo; end
# EOF
# bad
class Foo; end # EOF
# good
class Foo; end
# EOF
----
[#configurable-attributes-layouttrailingemptylines]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| EnforcedStyle
| `final_newline`
| `final_newline`, `final_blank_line`
|===
[#references-layouttrailingemptylines]
=== References
* https://rubystyle.guide#newline-eof
[#layouttrailingwhitespace]
== Layout/TrailingWhitespace
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| Always
| 0.49
| 1.0
|===
Looks for trailing whitespace in the source code.
[#examples-layouttrailingwhitespace]
=== Examples
[source,ruby]
----
# 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
----
[#allowinheredoc_-false-_default_-layouttrailingwhitespace]
==== AllowInHeredoc: false (default)
[source,ruby]
----
# The line in this example contains spaces after the 0.
# bad
code = <<~RUBY
x = 0
RUBY
# ok
code = <<~RUBY
x = 0 #{}
RUBY
# good
trailing_whitespace = ' '
code = <<~RUBY
x = 0#{trailing_whitespace}
RUBY
----
[#allowinheredoc_-true-layouttrailingwhitespace]
==== AllowInHeredoc: true
[source,ruby]
----
# The line in this example contains spaces after the 0.
# good
code = <<~RUBY
x = 0
RUBY
----
[#configurable-attributes-layouttrailingwhitespace]
=== Configurable attributes
|===
| Name | Default value | Configurable values
| AllowInHeredoc
| `false`
| Boolean
|===
[#references-layouttrailingwhitespace]
=== References
* https://rubystyle.guide#no-trailing-whitespace
include::../partials/cops_layout_footer.adoc[]