Whitespace
Whitespace
Indentation
-
Use soft-tabs with a
two-space indent.[link] -
Indent
whenas deep ascase.
[link]
case
when song.name == 'Misty'
puts 'Not again!'
when song.duration > 120
puts 'Too long!'
when Time.now.hour > 21
puts "It's too late"
else
song.play
end
kind = case year
when 1850..1889 then 'Blues'
when 1890..1909 then 'Ragtime'
when 1910..1929 then 'New Orleans Jazz'
when 1930..1939 then 'Swing'
when 1940..1950 then 'Bebop'
else 'Jazz'
end
- 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)
Trebuchet.current.launch?(ProgramEligibilityHelper::PROGRAM_TREBUCHET_FLAG) &&
is_in_program?(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
Inline
-
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
- Use spaces around operators; after commas,
colons, and semicolons; and around{and before}.
[link]
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]
result = func(a, b)
- 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]
# bad
var = "This #{ foobar } is interpolated."
# good
var = "This #{foobar} is interpolated."
- Don’t use extra whitespace in range
literals.[link]
# bad
(0 ... coll).each do |item|
# good
(0...coll).each do |item|
Newlines
- Add a new line after
ifconditions 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
def b
end
- Use a single empty line to break between
statements to break up methods into logical paragraphs internally.
[link]
def transformorize_car
car = manufacture(options)
t = transformer(robot, disguise)
car.after_market_mod!
t.transform(car)
car.assign_cool_name!
fleet.add(car)
car
end
- End each file with a newline. Don’t include
multiple newlines at the end of a file.
[link]
