Use UTF-8 as the source file encoding.
Each indentation level uses two spaces (also known as soft tabs). No hard tabs.
# bad-four Spaces
def some_method
do_something
End
# good
def some_method
do_something
End
Use unix-style line breaks. (*bsd/solaris/linux/osx users are covered by default, Windows users must be particularly cautious.)
- \ n is a line change, English is the LINEFEED,ASCII code is 0xA.
- \ r is enter, English is carriage return, ASCII code is 0xD.
- Windows down enter is \n\r,unix under \n,mac is \ r
If you are using Git you may want to add the following configuration settings to protect your project (avoid) the line breaks that Windows spreads:
$ git config--global core.autocrlf true
no use; To split statements and expressions. With this inference-one line uses an expression
# bad
puts ' foobar ', # superfluous semicolon
puts ' foo '; puts ' bar ' # Two expression on the same line
# good
puts ' Foobar '
puts ' foo '
puts ' bar '
puts ' foo ', ' bar ' # This applies to puts in particular
For class definitions that do not have content, use a Single-line class definition form whenever possible.
# Bad
class Fooerror < StandardError
end
# okish
class Fooerror < StandardError;
Good
Fooerror = class.new (StandardError)
Avoid single-line methods. Even if it is welcomed by some people, there are still some odd grammars that can be easily made wrong.
In any case-the line should be no more than one single-line method.
# bad Def Too_much; something Something_else, End # okish-notice that the ' the ' the ' the ', '
required
def no_braces _method; Body End
# Okish-notice That's second is optional
def No_braces_method, body, end
# okish-valid syntax , but no; Make it kind the hard to read
def some_method () body End
# good
def Some_method
the body end
An empty method is an exception to this rule.
The space next to the operator, after the comma, colon, and semicolon; before {and before}, most spaces may have nothing to do with Ruby interpretation (code), but the proper use of it is the key to making your code easier to read.
sum = 1 + 2
A, B = 1, 2
1 > 2 true:false; puts ' Hi '
[1, 2, 3].each {|e| puts E}
The only exception is when you use exponential operations:
# bad
e = m * C * * * 2
# good
e = m * c**2
{and} deserve additional clarification since they have been used for block and hash literals, as well as for embedding strings in the form of expressions.
Two styles for the hash literal are acceptable.
# good-space after {and before}
{one:1, two:2}
# Good-no spaces after {and before}
{one:1, two:2}
The first is slightly more readable (and arguably more popular in the Ruby community).
The second can increase the difference between block and hash visualization.
Whichever you choose--but it's best to keep it in line.
Currently, there are two choices for embedded expressions:
# good-no spaces
"string#{expr}"
# ok-arguably more readable
"string#{expr}"
The first style is extremely popular and usually suggests that you move closer to it. The second, on the other hand, is more readable (controversial).
Like hash-select a style and stay consistent.
There are no spaces (, after or],) before.
Some (ARG). Other
[1, 2, 3].length
! There are no spaces after that.
# bad,
something
# good
!something
When and case indentation depth consistent. I know a lot of people disagree with that, but it's the style recognized in "The Ruby programming Language" and "Programming Ruby".
# when
song.name = = ' Misty ' puts ' not again! '
When song.duration >
puts ' Too long! '
When Time.now.hour >
puts "It's too Late"
else Song.play end
# good
case When song.name = = ' Misty ' puts ' not
again!
' When song.duration >
puts ' Too long! '
When Time.now.hour >
puts "It's Too Late" else song.play end case when
song.name = = ' Misty ' puts ' not again! '
When Song.duraton >
puts ' Too long! '
When Time.now >
puts "It's too Late"
else
song.play
End
When assigning the result of a conditional expression to a variable, keep the branch indented at the same level.
# Bad-pretty Convoluted kind = case year, 1850..1889 then ' Blues ' when 1890..1909 then ' Ragtime ' when 191 0..1929 Then ' New Orleans Jazz ' when 1930..1939 then ' Swing ' when 1940..1950 then ' bebop ' else ' jazz ' end Resul t = if Some_cond calc_something else calc_something_else end # good-it ' s apparent what ' s going on kind = 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 result = if som E_cond calc_something Else Calc_something_else end # good (and a bit more width efficient Kind = case year when 1850..1889 then ' Blues ' when 1890..1909 then ' Ragtime ' when 1910..1929 then ' New Orl EANs Jazz ' when 1930..1939 then ' Swing ' when 1940..1950 then ' bebop ' Else ' "jazz ' end result = if Some_co
nd calc_something else Calc_something_else End
A blank line is used between the method definitions and a method is separated by a logical segment.
def some_method
data = Initialize (options)
data.manipulate!
Data.result
End
def some_methods
result
End
Avoid commas in the last argument of a method call, especially if the argument is not in another row.
# Bad-easier to Move/add/remove parameters, but still not preferred
Some_method (
size,
count,
Color,
)
# bad
some_method (size, count, color,)
# good
some_method (size, count, color)
When assigning a default value to a parameter of a method, use a space on both sides of the =:
# Bad
def some_method (arg1=:d efault, Arg2=nil, arg3=[])
# do something ...
End
# good
def some_method (arg1 =:d efault, arg2 = nil, arg3 = [])
# do something ...
End
Although several Ruby books suggest a first style, the second style is more common in practice (and arguably a bit more readable).
Avoid using line continuation characters when not needed. In practice,
Avoid using line-continuation characters in any case unless used for connection strings.
# bad Result
= 1-/
2
# good (but still ugly as hell) result
= 1 \
2
long_string = ' Part of the long string ' \ ' and second part of the
long string '
Use a consistent multiline approach to chain style. There are two popular styles in the Ruby community that are considered good
- . Start (option A) and trailing. (option B).
(option a) when a chained method call needs to continue on another line. Put it in the second line.
# Bad-need to consult the understand second line
One.two.three.
Four
# good-it ' s immediately clear what ' s going on the second line
One.two.three
. Four
(option B) when a chained method call continues on another line, the. Place the first line to identify the expression you want to continue.
# Bad-need to read ahead to the second line to know that chain continues
. One.two.three
T ' s immediately clear that the expression continues beyond the ' the ' the ' the '
.
Four
Here you can find a discussion of the advantages of these two alternative styles.
If a method call spans more than one line, the arguments are aligned. When the argument alignment is not appropriate because of the line width limit,
Single indent is also acceptable after the first line.
# starting point-too long
def send_mail (source)
Mailer.deliver (to: ' Bob@example.com ', from: ' Us@example.com ', Subject: ' Important message ', Body:source.text]
end
# bad (double indent)
def send_mail ( SOURCE)
Mailer.deliver (
to: ' bob@example.com ', from
: ' us@example.com ',
Subject: ' Important Message ',
body:source.text '
and
# good
def send_mail (source)
Mailer.deliver (to: ' Bob@example.com ', from
: ' us@example.com ',
Subject: ' Important message ',
Body:source.text]
End
# Good (normal indent)
def send_mail (source)
Mailer.deliver (
to: ' bob@example.com ', from
: ' Us@example.com ',
Subject: ' Important message ',
body:source.text
]
End
The elements of the array literals for the Zido span.
# bad-single Indent
menu_item = [' Spam ', ' Spam ', ' Spam ', ' Spam ', ' Spam ', ' Spam ', ' Spam ', ' Spam ', ' baked ',
' Spam ', ' Spam ', ' Spam ', ' Spam ', ' Spam ']
# good
menu_item = [
' Spam ', ' Spam ', ' Spam ', ' Spam ', ' Spam ', ' Spam ', ' Spam ', ' Spam ',
' baked beans ', ' Spam ', ' Spam ', ' Spam ', ' Spam ', ' Spam '
]
# good
menu_item =
[' Spam ', ' Spam ', ' Spam ', ' Spam ', ' Spam ', ' Spam ', ' Spam ', ' Spam ', ' baked ', ' beans ', ' Spam ', ' Spam ', ' Spam ', ' Spam '
Large values add underscores to improve their readability.
# bad-how many 0s are there?
num = 1000000
# Good-much easier to parse for the human brain
num = 1_000_000
Use RDOC and its conventions to compose API documents. Note Blocks and def are not separated by blank lines.
Each line is limited to 80 characters characters.
Avoid line trailing spaces.
Do not use block annotations. They cannot be guided by blanks (=begin must begin) and are not as easy to identify as ordinary annotations.
# bad
= = begin
Comment Line
another comment line = #
Good
# comment Line
# another Comme NT Line
Use the RDOC and its conventions in the API documentation. Do not add blank lines between the annotation code block and the def.
Keep each line less than 80 characters.
Avoid trailing spaces.