- 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 openingclassand closingendlines.
- Method names should start with a lower case letter, and should be in all lower-case:
method_name - Prefer
attr_reader,attr_writer,attr_accessorfor 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_activeoris_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
nilorfalse.
- Always use parentheses around arguments in a function definition.
- Use
&&and||for boolean expressions,andandorfor control flow. - Avoid multiline ternary operator (
?:), preferif. - Prefer methods over comparisons, e.g.:
- Prefer
unlessoverif !, except when there areelsiforelseclauses.t.empty?instead oft.size == 0t.zero?instead oft == 0
- Use single space after = or -