- Avoid conditional modifiers (lines that end with conditionals).
- Avoid multiple assignments per line (
one, two = 1, 2
). - Avoid organizational comments (
# Validations
). - Avoid ternary operators (
boolean ? true : false
). Use multi-lineif
instead to emphasize code branches. - Avoid explicit return statements.
- Avoid using semicolons.
- Avoid bang (!) method names. Prefer descriptive names.
- Don't use
self
explicitly anywhere except class methods (def self.method
) and assignments (self.attribute =
). - Prefer nested class and module definitions over the shorthand version Example
- Prefer
detect
overfind
. - Prefer
select
overfind_all
. - Prefer
map
overcollect
. - Prefer
reduce
overinject
. - Prefer double quotes for strings.
- Prefer
&&
and||
overand
andor
. - Prefer
!
overnot
. - Prefer
&:method_name
to{ |item| item.method_name }
for simple method calls. - Prefer
if
overunless
. - Use
_
for unused block parameters. - Prefix unused variables or parameters with underscore (
_
). - Use a leading underscore when defining instance variables for memoization.
- Use
%{}
for single-line strings needing interpolation and double-quotes. - Use
{...}
for single-line blocks. Usedo..end
for multi-line blocks. - Use
?
suffix for predicate methods. - Use
CamelCase
for classes and modules,snake_case
for variables and methods,SCREAMING_SNAKE_CASE
for constants. - Use
def self.method
, notdef Class.method
orclass << self
. - Use
def
with parentheses when there are arguments. - Don't use spaces after required keyword arguments. Example
- Use
each
, notfor
, for iteration. - Use a trailing comma after each item in a multi-line list, including the last item. Example
- Use heredocs for multi-line strings.
- Prefer
private
overprotected
for non-publicattr_reader
s,attr_writer
s, andattr_accessor
s. - Order class methods above instance methods.
- Prefer method invocation over instance variables.