When accessing elements from arrays or hashes, do not put spaces between the receiver name and the opeining brackets as it can make the code ambigous to read.
Using plaintext to render view is error prone and very unmaintanable. Prefer using templates instead of rendering inline text.
where
as a Hash RB-W1012Instead of generating fragments of SQL statements within ActiveRecord#where
, use it by passing in conditions
as a Hash.
enumerator
, rational
, complex
and thread
need not be require
d, since they can be directly used in the program.
enable
statement found after disable
RB-LI1034No # rubocop: enable
was found in the source code after the # rubocop: disable
comment. Since DeepSource respects issue silencing rules laid out by Rubocop, all issues in this file after the disable
statement will be silenced, and issues will
Literals used in conditions are pointless, as the programmer already knows if the condition will evaluate to true or false.
has_and_belongs_to_many
detected RB-RL1025Prefer has_many :through
to has_and_belongs_to_many
. Using has_many :through
allows additional attributes and validations on the join model.
index_with
can be used to create a hash from an enumerable RB-RL1059For transforming an enumerable into a hash where the values are the original elements, Rails provides the index_with
method.
Exception
RB-LI1029Error classes must inherit from RuntimeError
instead.
Rails.logger.debug
RB-W1009By default, Rails production environments use the :info
log level. At the :info
log level, Rails.logger.debug
statements do not result in log output.
However, Ruby must eagerly evaluate interpolated string arguments passed as method arguments. Passing a block to Rails.logger.debug
prevents costly evaluation of interpolated strings when no output would be produced anyway.
Self-assignment branches should be avoided as they are inefficient, unmaintanable and lack reability. Instead it is recommended to use single line conditionals if you need to make assignments.
%q
RB-ST1123The %q
syntax is used to define a string literal with single quotes. It is unnecessary when the string does not contain any single quotes or when you want to use double quotes instead.
For example, %q{Hello world}
is equivalent to 'Hello world', but %q{Hello's world}
is necessary to avoid having to escape the single quote character: 'Hello's world'.
If you don't need to use single quotes or want to use double quotes, you can use other string literal syntaxes such as %{}
or "".
For example, %"Hello world"
is equivalent to "Hello world", and %{Hello world}
is also equivalent to "Hello world".
rescue
RB-W1023Avoid bubbling up exceptions with useless rescue
s (which only re-raise the errors) in your code as they are
inefficient and can make the control flow difficult to understand.
In case you want to return nil
from a case body, it is better to return it explicitly. Otherwise the intent of the code may seem ambiguous.
require_relative
called with current file path RB-W1002Calling require_relative
with the path of the file it was called from doesn't perform any meaningful action since the file being executed is already in the LOAD_PATH
.
where
query method RB-C1016Prefer passing conditions to where and where.not as a hash over using fragments of SQL.
inquiry
method RB-C1017Prefer Ruby’s comparison operators over ActiveSupport
’s Array#inquiry
, and String#inquiry
.
Mailer
suffix RB-C1018Classes implementing mailing functionality (inheriting from ActionMailer::Base
or ApplicationMailer
) should be named with a Mailer
suffix.
Comparable#clamp
to limit value to a range RB-C1019When you want to limit a value to a certain range, prefer using Comparable#clamp
intead of combinations of
min
/max
and conditionals.
git
to declare files in gemspec RB-E1004Avoid using git ls-files
to produce lists of files. Downstreams often need to build your package in an environment that
does not have git (on purpose). Instead, use some pure Ruby alternatives, like Dir
or Dir.glob
.