- Use 2-space indentation (no tabs).
- Remove trailing whitespace.
- Use Unix-style line endings.
- Use spaces around operators, after commas, colons and semicolons, around
{
and before}
. - Use two spaces before statement modifiers (e.g. postfix
if
,unless
,while
,until
, orrescue
). - Use empty lines to break up a long method into logical paragraphs.
- Keep lines to fewer than 132 characters.
- Do not indent temporary or debugging code (e.g.
debugger
), but keep flush with left column for visibility. - Do not indent class access control statements (e.g.
protected
). That is, they should be aligned with the openingclass
and closingend
lines.
- Method names should start with a lower case letter, and should be in all lower-case:
method_name
- Prefer
attr_reader
,attr_writer
,attr_accessor
for simple attributes. - Accessor and mutator methods are simply the name of the attribute (without
get_
orset_
prefix). - Prefer
attribute=
overset_attribute
. - Methods that return a boolean should end in
?
and do not have theis_
prefix: preferactive?
overis_active
oris_active
. - Method names should end in
!
if the method:- Changes the object directly, as opposed to returning a modified copy of the object.
- Throws an exception on a failure condition, as opposed to returning
nil
orfalse
.
- Always use parentheses around arguments in a function definition.
- Use
&&
and||
for boolean expressions,and
andor
for control flow. - Avoid multiline ternary operator (
?:
), preferif
. - Prefer methods over comparisons, e.g.:
- Prefer
unless
overif !
, except when there areelsif
orelse
clauses.t.empty?
instead oft.size == 0
t.zero?
instead oft == 0
- Use single space after = or -