Prefer keyword arguments for arguments with a boolean default value; use logic: true
instead of logic = true
. Open
def tab_active?(_controller, _action, logic = true)
- Read upRead up
- Exclude checks
This cop checks for places where keyword arguments can be used instead of
boolean arguments when defining methods. respond_to_missing?
method is allowed by default.
These are customizable with AllowedMethods
option.
Safety:
This cop is unsafe because changing a method signature will implicitly change behaviour.
Example:
# bad
def some_method(bar = false)
puts bar
end
# bad - common hack before keyword args were introduced
def some_method(options = {})
bar = options.fetch(:bar, false)
puts bar
end
# good
def some_method(bar: false)
puts bar
end
Example: AllowedMethods: ['some_method']
# good
def some_method(bar = false)
puts bar
end
Do not use prefix _
for a variable that is used. Open
def tab_active?(_controller, _action, logic = true)
- Read upRead up
- Exclude checks
This cop checks for underscore-prefixed variables that are actually used.
Since block keyword arguments cannot be arbitrarily named at call
sites, the AllowKeywordBlockArguments
will allow use of underscore-
prefixed block keyword arguments.
Example: AllowKeywordBlockArguments: false (default)
# bad
[1, 2, 3].each do |_num|
do_something(_num)
end
query(:sales) do |_id:, revenue:, cost:|
{_id: _id, profit: revenue - cost}
end
# good
[1, 2, 3].each do |num|
do_something(num)
end
[1, 2, 3].each do |_num|
do_something # not using `_num`
end
Example: AllowKeywordBlockArguments: true
# good
query(:sales) do |_id:, revenue:, cost:|
{_id: _id, profit: revenue - cost}
end
Missing top-level documentation comment for module ApplicationHelper
. Open
module ApplicationHelper
- Read upRead up
- Exclude checks
This cop checks for missing top-level documentation of classes and modules. Classes with no body are exempt from the check and so are namespace modules - modules that have nothing in their bodies except classes, other modules, constant definitions or constant visibility declarations.
The documentation requirement is annulled if the class or module has a "#:nodoc:" comment next to it. Likewise, "#:nodoc: all" does the same for all its children.
Example:
# bad
class Person
# ...
end
module Math
end
# good
# Description/Explanation of Person class
class Person
# ...
end
# allowed
# Class without body
class Person
end
# Namespace - A namespace can be a class or a module
# Containing a class
module Namespace
# Description/Explanation of Person class
class Person
# ...
end
end
# Containing constant visibility declaration
module Namespace
class Private
end
private_constant :Private
end
# Containing constant definition
module Namespace
Public = Class.new
end
# Macro calls
module Namespace
extend Foo
end
Example: AllowedConstants: ['ClassMethods']
# good
module A
module ClassMethods
# ...
end
end
Prefer single-quoted strings when you don't need string interpolation or special symbols. Open
"active" if _controller.to_s == controller_name && action_name == _action.to_s && logic
- Read upRead up
- Exclude checks
Checks if uses of quotes match the configured preference.
Example: EnforcedStyle: single_quotes (default)
# bad
"No special symbols"
"No string interpolation"
"Just text"
# good
'No special symbols'
'No string interpolation'
'Just text'
"Wait! What's #{this}!"
Example: EnforcedStyle: double_quotes
# bad
'Just some text'
'No special chars or interpolation'
# good
"Just some text"
"No special chars or interpolation"
"Every string in #{project} uses double_quotes"
Do not use prefix _
for a variable that is used. Open
def tab_active?(_controller, _action, logic = true)
- Read upRead up
- Exclude checks
This cop checks for underscore-prefixed variables that are actually used.
Since block keyword arguments cannot be arbitrarily named at call
sites, the AllowKeywordBlockArguments
will allow use of underscore-
prefixed block keyword arguments.
Example: AllowKeywordBlockArguments: false (default)
# bad
[1, 2, 3].each do |_num|
do_something(_num)
end
query(:sales) do |_id:, revenue:, cost:|
{_id: _id, profit: revenue - cost}
end
# good
[1, 2, 3].each do |num|
do_something(num)
end
[1, 2, 3].each do |_num|
do_something # not using `_num`
end
Example: AllowKeywordBlockArguments: true
# good
query(:sales) do |_id:, revenue:, cost:|
{_id: _id, profit: revenue - cost}
end