Indent as deep as
case
.
[link]Align function parameters either all on
the same line or one per line.[link]# bad
def self.create_translation(phrase_id, phrase_key, target_locale,
value, user_id, do_xss_check, allow_verification)
...
end
# good
def self.create_translation(phrase_id,
phrase_key,
target_locale,
value,
user_id,
do_xss_check,
allow_verification)
...
end
# good
def self.create_translation(
phrase_id,
phrase_key,
target_locale,
value,
user_id,
do_xss_check,
allow_verification
)
...
end
Indent succeeding lines in multi-line
boolean expressions.[link]# bad
def is_eligible?(user)
program_not_expired
end
# good
def is_eligible?(user)
Trebuchet.current.launch?(ProgramEligibilityHelper::PROGRAM_TREBUCHET_FLAG) &&
is_in_program?(user) &&
program_not_expired
end
Never leave trailing whitespace.
[link]When making inline comments, include a
space between the end of the code and the start of your comment.
[link]# bad
result = func(a, b)# we might want to change b to c
# good
result = func(a, b) # we might want to change b to c
-
sum = 1 + 2
a, b = 1, 2
1 > 2 ? true : false; puts 'Hi'
[1, 2, 3].each { |e| puts e }
Never include a space before a comma.
[link]Do not include space inside block
parameter pipes. Include one space between parameters in a block.
Include one space outside block parameter pipes.
[link]# bad
{}.each { | x, y |puts x }
# good
{}.each { |x, y| puts x }
Do not leave space between
!
and its
argument.[link]!something
No spaces after
(
,[
or before]
,)
.
[link]some(arg).other
[1, 2, 3].length
Omit whitespace when doing
string interpolation.[link]var = "This #{ foobar } is interpolated."
var = "This #{foobar} is interpolated."
Add a new line after
if
conditions spanning
multiple lines to help differentiate between the conditions and the body.
[link]if @reservation_alteration.checkin == @reservation.start_date &&
@reservation_alteration.checkout == (@reservation.start_date + @reservation.nights)
redirect_to_alteration @reservation_alteration
end
Add a new line after conditionals,
blocks, case statements, etc.[link]if robot.is_awesome?
send_robot_present
end
robot.add_trait(:human_like_intelligence)
Don’t include newlines between areas
of different indentation (such as around class or module bodies).
[link]# bad
class Foo
def bar
# body omitted
end
end
# good
class Foo
def bar
# body omitted
end
end
Include one, but no more than one, new
line between methods.[link]def a
end
end
Use a single empty line to break between
statements to break up methods into logical paragraphs internally.
[link]