33> Style is what separates the good from the great. <br />
44> -- Bozhidar Batsov
55
6- One thing has always bothered me as Ruby developer - Python devs have
7- a great programming style reference (PEP-8) and we never got an
8- official guide documenting Ruby coding style and best practices. And I
9- do believe that style matters.
10-
11- This document was originally created when I, as the Technical Lead of
12- the company which I work for, was asked by our CTO to create some
13- internal documents describing good style and best practices for Ruby
14- programming. I started off by building upon
15- [ this existing style guide] ( https://github.com/chneukirchen/styleguide ) ,
16- since I concurred with many of the points in it. At some point I
17- decided that the work I was doing might be interesting to members of
18- the Ruby community in general and that the world had little need of
19- another internal company guideline. But the world could certainly
20- benefit from a community-driven and community-sanctioned set of
21- practices, idioms and style prescriptions for Ruby programming.
6+ One thing has always bothered me as Ruby developer - Python developers
7+ have a great programming style reference
8+ ([ PEP-8] ( http://www.python.org/dev/peps/pep-0008/ ) ) and we never got
9+ an official guide, documenting Ruby coding style and best
10+ practices. And I do believe that style matters. I also believe that
11+ such fine fellow like us Ruby developers, should be quite capable to
12+ produce this coveted document.
13+
14+ This guide started its life as our internal company coding guidelines
15+ (written by yours truly). At some point I decided that the work I was
16+ doing might be interesting to members of the Ruby community in general
17+ and that the world had little need of another internal company
18+ guideline. But the world could certainly benefit from a
19+ community-driven and community-sanctioned set of practices, idioms and
20+ style prescriptions for Ruby programming.
2221
2322Since the inception of the guide I've received a lot of feedback from
2423members of the exceptional Ruby community around the world. Thanks for
@@ -32,7 +31,7 @@ complementary
3231## Table of Contents
3332
3433* [ The Ruby Style Guide] ( #guide )
35- * [ Formatting ] ( #formatting )
34+ * [ Source Code Layout ] ( #layout )
3635 * [ Syntax] ( #syntax )
3736 * [ Naming] ( #naming )
3837 * [ Comments] ( #comments )
@@ -74,30 +73,39 @@ mind for now.
7473You can generate a PDF or an HTML copy of this guide using
7574[ Transmuter] ( https://github.com/TechnoGate/transmuter ) .
7675
77- <a name =" formatting " >
78- ## Formatting
76+ <a name =" layout " >
77+ ## Source Code Layout
7978
8079> Nearly everybody is convinced that every style but their own is
8180> ugly and unreadable. Leave out the "but their own" and they're
8281> probably right... <br />
8382> -- Jerry Coffin (on indentation)
8483
85- * Use UTF-8 as the source file encoding.
86- * Use two-space indent, no tabs. Tabs are represented by a different
87- number of spaces on various operating systems (and their
88- presentation can be manually configured as well) which usually
89- results in code that looks different than intended in some (many) people's
90- editors.
91- * Use Unix-style line endings. (Linux/OSX users are covered by default,
84+ * Use ` UTF-8 ` as the source file encoding.
85+ * Use two ** spaces** per indentation level.
86+
87+ ``` Ruby
88+ # good
89+ def some_method
90+ do_something
91+ end
92+
93+ # bad - four spaces
94+ def some_method
95+ do_something
96+ end
97+ ```
98+
99+ * Use Unix - style line endings. (* BSD / Solaris / Linux / OSX users are covered by default,
92100 Windows users have to be extra careful.)
93101 * If you' re using Git you might want to add the following
94102 configuration setting to protect your project from Windows line
95103 endings creeping in:
96104
97105 ```$ git config --global core.autocrlf true```
98106
99- * Use spaces around operators, after commas, colons and semicolons, around {
100- and before } . Whitespace might be (mostly) irrelevant to the Ruby
107+ * Use spaces around operators, after commas, colons and semicolons, around `{`
108+ and before `}` . Whitespace might be (mostly) irrelevant to the Ruby
101109 interpreter, but its proper use is the key to writing easily
102110 readable code.
103111
@@ -118,7 +126,7 @@ You can generate a PDF or an HTML copy of this guide using
118126 e = M * c**2
119127 ```
120128
121- * No spaces after (, [ or before ], ) .
129+ * No spaces after `(`, `[` or before `]`, `)` .
122130
123131 ```Ruby
124132 some(arg).other
@@ -171,54 +179,7 @@ You can generate a PDF or an HTML copy of this guide using
171179* Use RDoc and its conventions for API documentation. Don ' t put an
172180 empty line between the comment block and the `def`.
173181* Keep lines fewer than 80 characters.
174- * Emacs users might want to put this in their config
175- (e.g. `~/.emacs.d/init.el`):
176-
177- ```el
178- (setq whitespace-line-count 80
179- whitespace-style '(lines))
180- ```
181-
182- * Vim users might want to put this in their config
183- (e.g. `~/.vimrc`):
184-
185- ```vim
186- " VIM 7.3 + has support for highlighting a specified column.
187- if exists(' +colorcolumn' )
188- set colorcolumn= 80
189- else
190- " Emulate
191- au BufWinEnter * let w:m2=matchadd('ErrorMsg', '\% 80v.\+ ', -1)
192- endif
193- ```
194-
195- * Textmate
196-
197182* Avoid trailing whitespace.
198- * Emacs users might want to put this in their config (ideally
199- combine this with the previous example):
200-
201- ```el
202- (setq whitespace-style '(trailing space-before-tab
203- indentation space-after-tab))
204- ```
205-
206- * Vim users might want to put this in their `~/.vimrc`:
207-
208- ```vim
209- autocmd BufWritePre * :%s/\s\+ $//e
210- ```
211-
212- Or if you don't want vim to touch possibly vital space based files, use:
213-
214- ```vim
215- set listchars+=trail:░
216- ```
217-
218- Feel free to use some other character if you don't like the
219- suggested one.
220-
221- * Textmate users might want to take a look at the [Uber Glory bundle](https://github.com/glennr/uber-glory-tmbundle).
222183
223184<a name="syntax"/>
224185## Syntax
@@ -268,7 +229,7 @@ You can generate a PDF or an HTML copy of this guide using
268229 end
269230 ` ` `
270231
271- * Favor the ternary operator over if/then/else/end constructs.
232+ * Favor the ternary operator( ` ?: ` ) over ` if/then/else/end` constructs.
272233 It ' s more common and obviously more concise.
273234
274235 ```Ruby
@@ -281,7 +242,7 @@ You can generate a PDF or an HTML copy of this guide using
281242
282243* Use one expression per branch in a ternary operator. This
283244 also means that ternary operators must not be nested. Prefer
284- if/else constructs in these cases.
245+ ` if/else` constructs in these cases.
285246
286247 ```Ruby
287248 # bad
@@ -493,17 +454,25 @@ You can generate a PDF or an HTML copy of this guide using
493454 if v = array.grep(/foo/) ...
494455 ```
495456
496- * Use `||=` freely.
457+ * Use `||=` freely to initialize variables .
497458
498459 ```Ruby
499460 # set name to Bozhidar, only if it' s nil or false
500461 name ||= ' Bozhidar'
501462 ```
502463
503464* Don ' t use `||=` to initialize boolean variables. (Consider what
504- would happen if the current value happened to be `false`.)
465+ would happen if the current value happened to be `false`.)
466+
467+ ```Ruby
468+ # bad - would set enabled to true even if it was false
469+ enabled ||= true
470+
471+ # good
472+ enabled = true if enabled.nil?
473+ ```
505474
506- * Avoid using Perl-style special variables (like $0-9, $ `,
475+ * Avoid using Perl-style special variables (like ` $0-9`, `$` `,
507476 etc. ). They are quite cryptic and their use in anything but
508477 one-liner scripts is discouraged.
509478
@@ -540,27 +509,6 @@ You can generate a PDF or an HTML copy of this guide using
540509 (i.e. `Array#empty?`).
541510* The names of potentially "dangerous" methods (i.e. methods that modify `self` or the
542511 arguments, `exit!`, etc.) should end with an exclamation mark.
543- * The length of an identifier determines its scope. Use one-letter variables
544- for short block/method parameters, according to this scheme:
545-
546- a,b,c: any object
547- d: directory names
548- e: elements of an Enumerable
549- ex: rescued exceptions
550- f: files and file names
551- i,j: indexes
552- k: the key part of a hash entry
553- m: methods
554- o: any object
555- r: return values of short methods
556- s: strings
557- v: any value
558- v: the value part of a hash entry
559- x,y,z: numbers
560-
561- And in general, the first letter of the class name if all objects are of
562- that type.
563-
564512* When using `inject` with short blocks, name the arguments `|a, e|`
565513 (accumulator, element).
566514* When defining binary operators, name the argument `other`.
@@ -604,10 +552,8 @@ You can generate a PDF or an HTML copy of this guide using
604552
605553* Annotations should usually be written on the line immediately above
606554 the relevant code.
607-
608555* The annotation keyword is followed by a colon and a space, then a note
609556 describing the problem.
610-
611557* If multiple lines are required to describe the problem, subsequent
612558 lines should be indented two spaces after the `#`.
613559
@@ -631,19 +577,14 @@ You can generate a PDF or an HTML copy of this guide using
631577
632578* Use `TODO` to note missing features or functionality that should be
633579 added at a later date.
634-
635580* Use `FIXME` to note broken code that needs to be fixed.
636-
637581* Use `OPTIMIZE` to note slow or inefficient code that may cause
638582 performance problems.
639-
640583* Use `HACK` to note code smells where questionable coding practices
641584 were used and should be refactored away.
642-
643585* Use `REVIEW` to note anything that should be looked at to confirm it
644586 is working as intended. For example: `REVIEW: Are we sure this is how the
645587 client does X currently?`
646-
647588* Use other custom annotation keywords if it feels appropriate, but be
648589 sure to document them in your project's `README` or similar.
649590
@@ -672,7 +613,7 @@ You can generate a PDF or an HTML copy of this guide using
672613* Consider adding factory methods to provide additional sensible ways
673614 to create instances of a particular class .
674615* Prefer duck- typing over inheritance.
675- * Avoid the usage of class (@@ ) variables due to their " nasty" behavior
616+ * Avoid the usage of class (` @@ ` ) variables due to their " nasty" behavior
676617 in inheritance.
677618* Assign proper visibility levels to methods (` private` , ` protected` )
678619in accordance with their intended usage. Don ' t go off leaving
0 commit comments