Skip to content
Newer
Older
100644 4013 lines (3202 sloc) 94.4 KB
299d937 @bbatsov fixed a typo
authored Nov 23, 2011
1 # Prelude
dc31232 initial version
Bozhidar Batsov authored Sep 12, 2011
2
8282322 @JuanitoFatas Don't include a trailing slash in self-closing elements.
JuanitoFatas authored Apr 6, 2015
3 > Role models are important. <br>
5cf56b0 @bbatsov Update starting quote
authored May 30, 2013
4 > -- Officer Alex J. Murphy / RoboCop
15e47f1 improved the header a bit
Bozhidar Batsov authored Sep 14, 2011
5
e7ba4d3 @todb Rejustify README.md to 80 columns
todb authored Jul 18, 2014
6 One thing has always bothered me as a Ruby developer - Python developers have a
7 great programming style reference
a58f493 @bbatsov Start organizing the links
authored Sep 18, 2014
8 ([PEP-8][]) and we never got an official
e7ba4d3 @todb Rejustify README.md to 80 columns
todb authored Jul 18, 2014
9 guide, documenting Ruby coding style and best practices. And I do believe that
10 style matters. I also believe that a great hacker community, such as Ruby has,
11 should be quite capable of producing this coveted document.
6b1470c @bbatsov a few updates
authored Nov 23, 2011
12
299d937 @bbatsov fixed a typo
authored Nov 23, 2011
13 This guide started its life as our internal company Ruby coding guidelines
e7ba4d3 @todb Rejustify README.md to 80 columns
todb authored Jul 18, 2014
14 (written by yours truly). At some point I decided that the work I was doing
15 might be interesting to members of the Ruby community in general and that the
16 world had little need for another internal company guideline. But the world
17 could certainly benefit from a community-driven and community-sanctioned set of
18 practices, idioms and style prescriptions for Ruby programming.
19
20 Since the inception of the guide I've received a lot of feedback from members of
21 the exceptional Ruby community around the world. Thanks for all the suggestions
22 and the support! Together we can make a resource beneficial to each and every
23 Ruby developer out there.
24
25 By the way, if you're into Rails you might want to check out the complementary
a58f493 @bbatsov Start organizing the links
authored Sep 18, 2014
26 [Ruby on Rails Style Guide][rails-style-guide].
81f9b3a added a table of contents
Bozhidar Batsov authored Oct 21, 2011
27
1b18587 fixed a heading
Bozhidar Batsov authored Sep 26, 2011
28 # The Ruby Style Guide
3ff4a43 @bbatsov minor update
authored Sep 26, 2011
29
59a96d4 Add preface about real-world usage vs. ideal usage
Phillip Ridlen authored Oct 5, 2011
30 This Ruby style guide recommends best practices so that real-world Ruby
31 programmers can write code that can be maintained by other real-world Ruby
e7ba4d3 @todb Rejustify README.md to 80 columns
todb authored Jul 18, 2014
32 programmers. A style guide that reflects real-world usage gets used, and a style
33 guide that holds to an ideal that has been rejected by the people it is supposed
34 to help risks not getting used at all &ndash; no matter how good it is.
35
36 The guide is separated into several sections of related rules. I've tried to add
37 the rationale behind the rules (if it's omitted I've assumed it's pretty
38 obvious).
39
6848de8 @bbatsov Refill a paragraph
authored Sep 22, 2014
40 I didn't come up with all the rules out of nowhere - they are mostly
41 based on my extensive career as a professional software engineer,
42 feedback and suggestions from members of the Ruby community and
43 various highly regarded Ruby programming resources, such as
af6a7ba @0x01f7 Use the same name for book "Programming Ruby"
0x01f7 authored Dec 1, 2015
44 ["Programming Ruby"][pickaxe] and
6848de8 @bbatsov Refill a paragraph
authored Sep 22, 2014
45 ["The Ruby Programming Language"][trpl].
e7ba4d3 @todb Rejustify README.md to 80 columns
todb authored Jul 18, 2014
46
47 There are some areas in which there is no clear consensus in the Ruby community
48 regarding a particular style (like string literal quoting, spacing inside hash
49 literals, dot position in multi-line method chaining, etc.). In such scenarios
50 all popular styles are acknowledged and it's up to you to pick one and apply it
51 consistently.
52
703ee03 @bbatsov Add a note about coding conventions evolution
authored Sep 18, 2014
53 This style guide evolves over time as additional conventions are
54 identified and past conventions are rendered obsolete by changes in
55 Ruby itself.
56
57 Many projects have their own coding style guidelines (often derived
58 from this guide). In the event of any conflicts, such
59 project-specific guides take precedence for that project.
3ff4a43 @bbatsov minor update
authored Sep 26, 2011
60
59a8349 @bbatsov removed the HTML/PDF generation code
authored Oct 29, 2011
61 You can generate a PDF or an HTML copy of this guide using
ef0852f @bbatsov Replace the mention of Transmuter with Pandoc
authored Apr 1, 2016
62 [Pandoc][].
626ce31 @kalbasit Add a note about requirements in the README.
kalbasit authored Oct 1, 2011
63
a58f493 @bbatsov Start organizing the links
authored Sep 18, 2014
64 [RuboCop][] is a code analyzer, based on this
e7ba4d3 @todb Rejustify README.md to 80 columns
todb authored Jul 18, 2014
65 style guide.
b1936bd @bbatsov mentioned rubocop
authored Dec 20, 2012
66
df777f3 @bbatsov added a link to a Chinese translation of the guide
authored Nov 18, 2012
67 Translations of the guide are available in the following languages:
68
b377526 @bbatsov mention the Chinese Traditional translation as well
authored Nov 18, 2012
69 * [Chinese Simplified](https://github.com/JuanitoFatas/ruby-style-guide/blob/master/README-zhCN.md)
5431a27 @bbatsov fixed a typo
authored Nov 18, 2012
70 * [Chinese Traditional](https://github.com/JuanitoFatas/ruby-style-guide/blob/master/README-zhTW.md)
5ffde4b @ReadmeCritic Update redirects
ReadmeCritic authored Nov 1, 2015
71 * [French](https://github.com/gauthier-delacroix/ruby-style-guide/blob/master/README-frFR.md)
453dbb1 @arbox Changed the link to the German version due to replacement of this ver…
arbox authored May 13, 2015
72 * [German](https://github.com/arbox/de-ruby-style-guide/blob/master/README-deDE.md)
77045e9 @5470x3 Add a link to the Japanese version.
5470x3 authored Feb 14, 2014
73 * [Japanese](https://github.com/fortissimo1997/ruby-style-guide/blob/japanese/README.ja.md)
1d21dcd @dalzony Change the url of korean with iso guide.
dalzony authored Dec 8, 2014
74 * [Korean](https://github.com/dalzony/ruby-style-guide/blob/master/README-koKR.md)
b0d624f @arbox Added a Russian translations into the tranlsations list.
arbox authored Jun 29, 2014
75 * [Portuguese](https://github.com/rubensmabueno/ruby-style-guide/blob/master/README-PT-BR.md)
76 * [Russian](https://github.com/arbox/ruby-style-guide/blob/master/README-ruRU.md)
ab09794 @bbatsov Mention Spanish translation
authored Dec 2, 2013
77 * [Spanish](https://github.com/alemohamad/ruby-style-guide/blob/master/README-esLA.md)
6c97a93 @haibt Add Vietnamese version link
haibt authored Jan 13, 2014
78 * [Vietnamese](https://github.com/scrum2b/ruby-style-guide/blob/master/README-viVN.md)
df777f3 @bbatsov added a link to a Chinese translation of the guide
authored Nov 18, 2012
79
39eea48 @bbatsov added table of contents
authored May 1, 2012
80 ## Table of Contents
81
82 * [Source Code Layout](#source-code-layout)
83 * [Syntax](#syntax)
84 * [Naming](#naming)
85 * [Comments](#comments)
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
86 * [Comment Annotations](#comment-annotations)
eaa684c @maurogeorge Keep consistency on the Classes & Modules link
maurogeorge authored Nov 11, 2015
87 * [Classes & Modules](#classes--modules)
39eea48 @bbatsov added table of contents
authored May 1, 2012
88 * [Exceptions](#exceptions)
89 * [Collections](#collections)
bd6f8a2 @dpisarewski Add a type-checking rule for integer numbers (#564)
dpisarewski authored Apr 28, 2016
90 * [Numbers](#numbers)
39eea48 @bbatsov added table of contents
authored May 1, 2012
91 * [Strings](#strings)
92 * [Regular Expressions](#regular-expressions)
93 * [Percent Literals](#percent-literals)
94 * [Metaprogramming](#metaprogramming)
95 * [Misc](#misc)
0a97a5b @bbatsov Add tools section
authored Apr 15, 2013
96 * [Tools](#tools)
39eea48 @bbatsov added table of contents
authored May 1, 2012
97
6b1470c @bbatsov a few updates
authored Nov 23, 2011
98 ## Source Code Layout
dc31232 initial version
Bozhidar Batsov authored Sep 12, 2011
99
61a9061 Convert quotes to Markdown blockquote syntax
Phillip Ridlen authored Oct 7, 2011
100 > Nearly everybody is convinced that every style but their own is
101 > ugly and unreadable. Leave out the "but their own" and they're
8282322 @JuanitoFatas Don't include a trailing slash in self-closing elements.
JuanitoFatas authored Apr 6, 2015
102 > probably right... <br>
61a9061 Convert quotes to Markdown blockquote syntax
Phillip Ridlen authored Oct 7, 2011
103 > -- Jerry Coffin (on indentation)
1b18587 fixed a heading
Bozhidar Batsov authored Sep 26, 2011
104
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
105 * <a name="utf-8"></a>
106 Use `UTF-8` as the source file encoding.
107 <sup>[[link](#utf-8)]</sup>
108
109 * <a name="spaces-indentation"></a>
110 Use two **spaces** per indentation level (aka soft tabs). No hard tabs.
111 <sup>[[link](#spaces-indentation)]</sup>
6b1470c @bbatsov a few updates
authored Nov 23, 2011
112
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
113 ```Ruby
114 # bad - four spaces
115 def some_method
599839e Always place bad examples before good ones
Cecile Veneziani authored Apr 6, 2013
116 do_something
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
117 end
118
119 # good
120 def some_method
121 do_something
122 end
123 ```
6b1470c @bbatsov a few updates
authored Nov 23, 2011
124
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
125 * <a name="crlf"></a>
126 Use Unix-style line endings. (*BSD/Solaris/Linux/OS X users are covered by
127 default, Windows users have to be extra careful.)
128 <sup>[[link](#crlf)]</sup>
129
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
130 * If you're using Git you might want to add the following
a08f5cc @bbatsov fixed a few errors
authored Sep 22, 2011
131 configuration setting to protect your project from Windows line
47c97ea @bbatsov better wording
authored Sep 22, 2011
132 endings creeping in:
a08f5cc @bbatsov fixed a few errors
authored Sep 22, 2011
133
1a27fa5 @lee-dohm Put fenced code block around shell command
lee-dohm authored Aug 9, 2013
134 ```bash
135 $ git config --global core.autocrlf true
136 ```
a08f5cc @bbatsov fixed a few errors
authored Sep 22, 2011
137
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
138 * <a name="no-semicolon"></a>
139 Don't use `;` to separate statements and expressions. As a corollary - use one
140 expression per line.
141 <sup>[[link](#no-semicolon)]</sup>
35ea330 @bbatsov Discourage use of semicolons
authored Apr 16, 2013
142
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
143 ```Ruby
144 # bad
145 puts 'foobar'; # superfluous semicolon
35ea330 @bbatsov Discourage use of semicolons
authored Apr 16, 2013
146
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
147 puts 'foo'; puts 'bar' # two expressions on the same line
35ea330 @bbatsov Discourage use of semicolons
authored Apr 16, 2013
148
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
149 # good
150 puts 'foobar'
35ea330 @bbatsov Discourage use of semicolons
authored Apr 16, 2013
151
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
152 puts 'foo'
153 puts 'bar'
35ea330 @bbatsov Discourage use of semicolons
authored Apr 16, 2013
154
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
155 puts 'foo', 'bar' # this applies to puts in particular
156 ```
35ea330 @bbatsov Discourage use of semicolons
authored Apr 16, 2013
157
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
158 * <a name="single-line-classes"></a>
159 Prefer a single-line format for class definitions with no body.
160 <sup>[[link](#single-line-classes)]</sup>
15ea2b1 @lee-dohm Add acceptable uses for ; in one-line classes and methods
lee-dohm authored Apr 28, 2013
161
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
162 ```Ruby
163 # bad
164 class FooError < StandardError
165 end
15ea2b1 @lee-dohm Add acceptable uses for ; in one-line classes and methods
lee-dohm authored Apr 28, 2013
166
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
167 # okish
168 class FooError < StandardError; end
c4e3138 @bbatsov Prefer proc over Proc.new
authored May 28, 2013
169
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
170 # good
171 FooError = Class.new(StandardError)
172 ```
15ea2b1 @lee-dohm Add acceptable uses for ; in one-line classes and methods
lee-dohm authored Apr 28, 2013
173
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
174 * <a name="no-single-line-methods"></a>
175 Avoid single-line methods. Although they are somewhat popular in the wild,
176 there are a few peculiarities about their definition syntax that make their
177 use undesirable. At any rate - there should be no more than one expression in
178 a single-line method.
179 <sup>[[link](#no-single-line-methods)]</sup>
2d35de9 @bbatsov Add rules about one-liner methods
authored Apr 28, 2013
180
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
181 ```Ruby
182 # bad
183 def too_much; something; something_else; end
2d35de9 @bbatsov Add rules about one-liner methods
authored Apr 28, 2013
184
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
185 # okish - notice that the first ; is required
186 def no_braces_method; body end
2d35de9 @bbatsov Add rules about one-liner methods
authored Apr 28, 2013
187
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
188 # okish - notice that the second ; is optional
189 def no_braces_method; body; end
2d35de9 @bbatsov Add rules about one-liner methods
authored Apr 28, 2013
190
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
191 # okish - valid syntax, but no ; makes it kind of hard to read
192 def some_method() body end
2d35de9 @bbatsov Add rules about one-liner methods
authored Apr 28, 2013
193
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
194 # good
195 def some_method
196 body
197 end
198 ```
2d35de9 @bbatsov Add rules about one-liner methods
authored Apr 28, 2013
199
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
200 One exception to the rule are empty-body methods.
2d35de9 @bbatsov Add rules about one-liner methods
authored Apr 28, 2013
201
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
202 ```Ruby
203 # good
204 def no_op; end
205 ```
2d35de9 @bbatsov Add rules about one-liner methods
authored Apr 28, 2013
206
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
207 * <a name="spaces-operators"></a>
f88e1de @esdoppio Gather rules about spaces around braces in one place (#560)
esdoppio authored Apr 28, 2016
208 Use spaces around operators, after commas, colons and semicolons.
209 Whitespace might be (mostly) irrelevant to the Ruby interpreter,
210 but its proper use is the key to writing easily readable code.
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
211 <sup>[[link](#spaces-operators)]</sup>
dc31232 initial version
Bozhidar Batsov authored Sep 12, 2011
212
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
213 ```Ruby
214 sum = 1 + 2
215 a, b = 1, 2
835281c @bbatsov [Fix #314] Resolve a rule conflict
authored Apr 25, 2014
216 class FooError < StandardError; end
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
217 ```
dc31232 initial version
Bozhidar Batsov authored Sep 12, 2011
218
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
219 The only exception, regarding operators, is the exponent operator:
ea832ae Add formatting exception for exponent operator.
Phillip Ridlen authored Sep 28, 2011
220
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
221 ```Ruby
222 # bad
223 e = M * c ** 2
ea832ae Add formatting exception for exponent operator.
Phillip Ridlen authored Sep 28, 2011
224
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
225 # good
226 e = M * c**2
227 ```
ea832ae Add formatting exception for exponent operator.
Phillip Ridlen authored Sep 28, 2011
228
f88e1de @esdoppio Gather rules about spaces around braces in one place (#560)
esdoppio authored Apr 27, 2016
229 * <a name="spaces-braces"></a>
230 No spaces after `(`, `[` or before `]`, `)`.
231 Use spaces around `{` and before `}`.
232 <sup>[[link](#spaces-braces)]</sup>
233
234 ```Ruby
235 # bad
236 some( arg ).other
237 [ 1, 2, 3 ].each{|e| puts e}
238
239 # good
240 some(arg).other
241 [1, 2, 3].each { |e| puts e }
242 ```
243
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
244 `{` and `}` deserve a bit of clarification, since they are used
7908152 @onebree include 1 method of padded-spacing string interpolation
onebree authored Apr 16, 2015
245 for block and hash literals, as well as string interpolation.
f88e1de @esdoppio Gather rules about spaces around braces in one place (#560)
esdoppio authored Apr 27, 2016
246
7908152 @onebree include 1 method of padded-spacing string interpolation
onebree authored Apr 16, 2015
247 For hash literals two styles are considered acceptable.
f88e1de @esdoppio Gather rules about spaces around braces in one place (#560)
esdoppio authored Apr 27, 2016
248 The first variant is slightly more readable (and arguably more
249 popular in the Ruby community in general). The second variant has
250 the advantage of adding visual difference between block and hash
251 literals. Whichever one you pick - apply it consistently.
6d73619 @bbatsov Add a bit more rules about the use of {}
authored Apr 30, 2013
252
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
253 ```Ruby
254 # good - space after { and before }
255 { one: 1, two: 2 }
6d73619 @bbatsov Add a bit more rules about the use of {}
authored Apr 30, 2013
256
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
257 # good - no space after { and before }
258 {one: 1, two: 2}
259 ```
6d73619 @bbatsov Add a bit more rules about the use of {}
authored Apr 30, 2013
260
f88e1de @esdoppio Gather rules about spaces around braces in one place (#560)
esdoppio authored Apr 27, 2016
261 With interpolated expressions, there should be no padded-spacing inside the braces.
dc31232 initial version
Bozhidar Batsov authored Sep 12, 2011
262
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
263 ```Ruby
b1777d3 @etdev Clarify spaces around parens/brackets rule
etdev authored Aug 4, 2015
264 # bad
f88e1de @esdoppio Gather rules about spaces around braces in one place (#560)
esdoppio authored Apr 27, 2016
265 "From: #{ user.first_name }, #{ user.last_name }"
b1777d3 @etdev Clarify spaces around parens/brackets rule
etdev authored Aug 4, 2015
266
267 # good
f88e1de @esdoppio Gather rules about spaces around braces in one place (#560)
esdoppio authored Apr 27, 2016
268 "From: #{user.first_name}, #{user.last_name}"
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
269 ```
dc31232 initial version
Bozhidar Batsov authored Sep 12, 2011
270
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
271 * <a name="no-space-bang"></a>
272 No space after `!`.
273 <sup>[[link](#no-space-bang)]</sup>
8c23bad @bbatsov No space after !
authored Oct 10, 2013
274
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
275 ```Ruby
276 # bad
277 ! something
8c23bad @bbatsov No space after !
authored Oct 10, 2013
278
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
279 # good
280 !something
281 ```
8c23bad @bbatsov No space after !
authored Oct 10, 2013
282
9993e76 @bbatsov Update an anchor
authored Aug 18, 2014
283 * <a name="no-space-inside-range-literals"></a>
a614370 @bbatsov [Fix #347] Add a rule regarding space inside range literals
authored Aug 18, 2014
284 No space inside range literals.
9993e76 @bbatsov Update an anchor
authored Aug 18, 2014
285 <sup>[[link](#no-space-inside-range-literals)]</sup>
a614370 @bbatsov [Fix #347] Add a rule regarding space inside range literals
authored Aug 18, 2014
286
287 ```Ruby
288 # bad
289 1 .. 3
290 'a' ... 'z'
291
292 # good
293 1..3
98dc6d2 @bbatsov [Fix #381] Use the correct range literal
authored Dec 5, 2014
294 'a'...'z'
a614370 @bbatsov [Fix #347] Add a rule regarding space inside range literals
authored Aug 18, 2014
295 ```
296
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
297 * <a name="indent-when-to-case"></a>
c565a42 @etdev Clean up wording slightly
etdev authored Oct 13, 2015
298 Indent `when` as deep as `case`. This is the style established in both
299 "The Ruby Programming Language" and "Programming Ruby".
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
300 <sup>[[link](#indent-when-to-case)]</sup>
dc31232 initial version
Bozhidar Batsov authored Sep 12, 2011
301
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
302 ```Ruby
303 # bad
304 case
3c42fef @bbatsov enforced the single quoted strings rule across all examples in the guide
authored Oct 2, 2011
305 when song.name == 'Misty'
306 puts 'Not again!'
00032bf @restorer Fix examples for "case"
restorer authored Sep 16, 2011
307 when song.duration > 120
3c42fef @bbatsov enforced the single quoted strings rule across all examples in the guide
authored Oct 2, 2011
308 puts 'Too long!'
00032bf @restorer Fix examples for "case"
restorer authored Sep 16, 2011
309 when Time.now.hour > 21
0356bdd @Heliosmaster fixed instruction about single quotes + typo
Heliosmaster authored Oct 3, 2011
310 puts "It's too late"
2681d4b added highlighting to all examples
Bozhidar Batsov authored Sep 13, 2011
311 else
312 song.play
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
313 end
dc31232 initial version
Bozhidar Batsov authored Sep 12, 2011
314
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
315 # good
316 case
317 when song.name == 'Misty'
318 puts 'Not again!'
319 when song.duration > 120
320 puts 'Too long!'
321 when Time.now.hour > 21
322 puts "It's too late"
323 else
324 song.play
325 end
326 ```
efc1b56 @bbatsov [Fix #179] Add examples of multi-line conditional assignment
authored Sep 17, 2013
327
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
328 * <a name="indent-conditional-assignment"></a>
329 When assigning the result of a conditional expression to a variable,
330 preserve the usual alignment of its branches.
331 <sup>[[link](#indent-conditional-assignment)]</sup>
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
332
333 ```Ruby
334 # bad - pretty convoluted
335 kind = case year
336 when 1850..1889 then 'Blues'
337 when 1890..1909 then 'Ragtime'
338 when 1910..1929 then 'New Orleans Jazz'
339 when 1930..1939 then 'Swing'
340 when 1940..1950 then 'Bebop'
341 else 'Jazz'
342 end
343
344 result = if some_cond
345 calc_something
346 else
347 calc_something_else
348 end
349
350 # good - it's apparent what's going on
351 kind = case year
352 when 1850..1889 then 'Blues'
353 when 1890..1909 then 'Ragtime'
354 when 1910..1929 then 'New Orleans Jazz'
355 when 1930..1939 then 'Swing'
356 when 1940..1950 then 'Bebop'
357 else 'Jazz'
358 end
359
360 result = if some_cond
361 calc_something
362 else
363 calc_something_else
364 end
365
366 # good (and a bit more width efficient)
367 kind =
368 case year
efc1b56 @bbatsov [Fix #179] Add examples of multi-line conditional assignment
authored Sep 17, 2013
369 when 1850..1889 then 'Blues'
370 when 1890..1909 then 'Ragtime'
371 when 1910..1929 then 'New Orleans Jazz'
372 when 1930..1939 then 'Swing'
373 when 1940..1950 then 'Bebop'
374 else 'Jazz'
375 end
376
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
377 result =
378 if some_cond
75ea6d1 @bbatsov Extend an example
authored Sep 17, 2013
379 calc_something
380 else
381 calc_something_else
382 end
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
383 ```
75ea6d1 @bbatsov Extend an example
authored Sep 17, 2013
384
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
385 * <a name="empty-lines-between-methods"></a>
c565a42 @etdev Clean up wording slightly
etdev authored Oct 13, 2015
386 Use empty lines between method definitions and also to break up methods
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
387 into logical paragraphs internally.
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
388 <sup>[[link](#empty-lines-between-methods)]</sup>
dc31232 initial version
Bozhidar Batsov authored Sep 12, 2011
389
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
390 ```Ruby
391 def some_method
392 data = initialize(options)
dc31232 initial version
Bozhidar Batsov authored Sep 12, 2011
393
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
394 data.manipulate!
74a6727 Clarify usage of empty lines inside methods.
Daniel Esplin authored Oct 17, 2011
395
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
396 data.result
397 end
dc31232 initial version
Bozhidar Batsov authored Sep 12, 2011
398
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
399 def some_method
400 result
401 end
402 ```
8a50545 @mitio Remove trailing whitespace, fix punctuation and minor mistakes.
mitio authored Sep 18, 2011
403
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
404 * <a name="no-trailing-params-comma"></a>
405 Avoid comma after the last parameter in a method call, especially when the
41c56d6 @jonas054 Remove trailing comma rule for method definitions.
jonas054 authored Jan 11, 2014
406 parameters are not on separate lines.
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
407 <sup>[[link](#no-trailing-params-comma)]</sup>
426e890 @jonas054 Add rules about trailing commas.
jonas054 authored Jan 11, 2014
408
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
409 ```Ruby
410 # bad - easier to move/add/remove parameters, but still not preferred
411 some_method(
412 size,
413 count,
414 color,
415 )
426e890 @jonas054 Add rules about trailing commas.
jonas054 authored Jan 11, 2014
416
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
417 # bad
418 some_method(size, count, color, )
426e890 @jonas054 Add rules about trailing commas.
jonas054 authored Jan 11, 2014
419
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
420 # good
421 some_method(size, count, color)
422 ```
426e890 @jonas054 Add rules about trailing commas.
jonas054 authored Jan 11, 2014
423
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
424 * <a name="spaces-around-equals"></a>
425 Use spaces around the `=` operator when assigning default values to method
426 parameters:
427 <sup>[[link](#spaces-around-equals)]</sup>
c0098f1 @bbatsov Moved a few rules to the Layout section
authored Apr 30, 2013
428
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
429 ```Ruby
430 # bad
431 def some_method(arg1=:default, arg2=nil, arg3=[])
432 # do something...
433 end
c0098f1 @bbatsov Moved a few rules to the Layout section
authored Apr 30, 2013
434
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
435 # good
436 def some_method(arg1 = :default, arg2 = nil, arg3 = [])
437 # do something...
438 end
439 ```
c0098f1 @bbatsov Moved a few rules to the Layout section
authored Apr 30, 2013
440
89dc612 @yous Keep line length to 80
yous authored Jul 25, 2014
441 While several Ruby books suggest the first style, the second is much more
442 prominent in practice (and arguably a bit more readable).
c0098f1 @bbatsov Moved a few rules to the Layout section
authored Apr 30, 2013
443
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
444 * <a name="no-trailing-backslash"></a>
445 Avoid line continuation `\` where not required. In practice, avoid using
97cef51 @bbatsov Line continuation is OK for multi-line strings
authored Aug 23, 2013
446 line continuations for anything but string concatenation.
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
447 <sup>[[link](#no-trailing-backslash)]</sup>
c0098f1 @bbatsov Moved a few rules to the Layout section
authored Apr 30, 2013
448
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
449 ```Ruby
450 # bad
451 result = 1 - \
452 2
c0098f1 @bbatsov Moved a few rules to the Layout section
authored Apr 30, 2013
453
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
454 # good (but still ugly as hell)
455 result = 1 \
456 - 2
97cef51 @bbatsov Line continuation is OK for multi-line strings
authored Aug 23, 2013
457
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
458 long_string = 'First part of the long string' \
459 ' and second part of the long string'
460 ```
c0098f1 @bbatsov Moved a few rules to the Layout section
authored Apr 30, 2013
461
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
462 * <a name="consistent-multi-line-chains"></a>
463 Adopt a consistent multi-line method chaining style. There are two
464 popular styles in the Ruby community, both of which are considered
465 good - leading `.` (Option A) and trailing `.` (Option B).
466 <sup>[[link](#consistent-multi-line-chains)]</sup>
c0098f1 @bbatsov Moved a few rules to the Layout section
authored Apr 30, 2013
467
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
468 * **(Option A)** When continuing a chained method invocation on
469 another line keep the `.` on the second line.
c0098f1 @bbatsov Moved a few rules to the Layout section
authored Apr 30, 2013
470
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
471 ```Ruby
472 # bad - need to consult first line to understand second line
473 one.two.three.
474 four
fefe83d @bbatsov [#169 #176] Acknowledge trailing . as a good style as well
authored Feb 18, 2014
475
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
476 # good - it's immediately clear what's going on the second line
477 one.two.three
478 .four
479 ```
fefe83d @bbatsov [#169 #176] Acknowledge trailing . as a good style as well
authored Feb 18, 2014
480
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
481 * **(Option B)** When continuing a chained method invocation on another line,
482 include the `.` on the first line to indicate that the
483 expression continues.
fefe83d @bbatsov [#169 #176] Acknowledge trailing . as a good style as well
authored Feb 18, 2014
484
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
485 ```Ruby
486 # bad - need to read ahead to the second line to know that the chain continues
487 one.two.three
488 .four
fefe83d @bbatsov [#169 #176] Acknowledge trailing . as a good style as well
authored Feb 18, 2014
489
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
490 # good - it's immediately clear that the expression continues beyond the first line
491 one.two.three.
492 four
493 ```
c0098f1 @bbatsov Moved a few rules to the Layout section
authored Apr 30, 2013
494
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
495 A discussion on the merits of both alternative styles can be found
496 [here](https://github.com/bbatsov/ruby-style-guide/pull/176).
a160cab @bbatsov Link leading vs trailing dot discussion
authored Feb 18, 2014
497
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
498 * <a name="no-double-indent"></a>
499 Align the parameters of a method call if they span more than one
500 line. When aligning parameters is not appropriate due to line-length
501 constraints, single indent for the lines after the first is also
502 acceptable.
503 <sup>[[link](#no-double-indent)]</sup>
4e98267 fixes #80 - Indentation on multi-line method calls
Bozhidar Batsov authored Mar 1, 2012
504
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
505 ```Ruby
506 # starting point (line is too long)
507 def send_mail(source)
508 Mailer.deliver(to: 'bob@example.com', from: 'us@example.com', subject: 'Important message', body: source.text)
509 end
7f6459b @bbatsov Refine multi-line params indentation rule
authored Jun 24, 2013
510
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
511 # bad (double indent)
512 def send_mail(source)
513 Mailer.deliver(
7f6459b @bbatsov Refine multi-line params indentation rule
authored Jun 24, 2013
514 to: 'bob@example.com',
515 from: 'us@example.com',
516 subject: 'Important message',
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
517 body: source.text)
518 end
519
520 # good
521 def send_mail(source)
522 Mailer.deliver(to: 'bob@example.com',
523 from: 'us@example.com',
524 subject: 'Important message',
525 body: source.text)
526 end
527
528 # good (normal indent)
529 def send_mail(source)
530 Mailer.deliver(
531 to: 'bob@example.com',
532 from: 'us@example.com',
533 subject: 'Important message',
534 body: source.text
535 )
536 end
537 ```
4e98267 fixes #80 - Indentation on multi-line method calls
Bozhidar Batsov authored Mar 1, 2012
538
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
539 * <a name="align-multiline-arrays"></a>
540 Align the elements of array literals spanning multiple lines.
541 <sup>[[link](#align-multiline-arrays)]</sup>
d950f0c @bbatsov Add a rule about the indentation of multi-line array literals
authored Oct 11, 2013
542
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
543 ```Ruby
544 # bad - single indent
545 menu_item = ['Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam',
546 'Baked beans', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam']
d950f0c @bbatsov Add a rule about the indentation of multi-line array literals
authored Oct 11, 2013
547
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
548 # good
549 menu_item = [
550 'Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam',
551 'Baked beans', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam'
552 ]
d950f0c @bbatsov Add a rule about the indentation of multi-line array literals
authored Oct 11, 2013
553
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
554 # good
555 menu_item =
556 ['Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam',
557 'Baked beans', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam']
558 ```
d950f0c @bbatsov Add a rule about the indentation of multi-line array literals
authored Oct 11, 2013
559
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
560 * <a name="underscores-in-numerics"></a>
561 Add underscores to large numeric literals to improve their readability.
562 <sup>[[link](#underscores-in-numerics)]</sup>
6db7265 @bbatsov added an advice about big numeric literals
authored Nov 15, 2012
563
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
564 ```Ruby
565 # bad - how many 0s are there?
566 num = 1000000
6db7265 @bbatsov added an advice about big numeric literals
authored Nov 14, 2012
567
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
568 # good - much easier to parse for the human brain
569 num = 1_000_000
570 ```
6db7265 @bbatsov added an advice about big numeric literals
authored Nov 14, 2012
571
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
572 * <a name="rdoc-conventions"></a>
88bfea5 @Neodelf Improve some examples and apply some formatting corrections
Neodelf authored Mar 26, 2016
573 Use [Rdoc][rdoc] and its conventions for API documentation. Don't put an
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
574 empty line between the comment block and the `def`.
575 <sup>[[link](#rdoc-conventions)]</sup>
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
576
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
577 * <a name="80-character-limits"></a>
578 Limit lines to 80 characters.
579 <sup>[[link](#80-character-limits)]</sup>
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
580
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
581 * <a name="no-trailing-whitespace"></a>
582 Avoid trailing whitespace.
583 <sup>[[link](#no-trailing-whitespace)]</sup>
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
584
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
585 * <a name="newline-eof"></a>
586 End each file with a newline.
587 <sup>[[link](#newline-eof)]</sup>
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
588
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
589 * <a name="no-block-comments"></a>
590 Don't use block comments. They cannot be preceded by whitespace and are not
591 as easy to spot as regular comments.
592 <sup>[[link](#no-block-comments)]</sup>
7e089fa @bbatsov Don't use block comments
authored May 1, 2013
593
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
594 ```Ruby
595 # bad
7e1f61d @ruan-brandao Update README.md
ruan-brandao authored Mar 31, 2014
596 =begin
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
597 comment line
598 another comment line
7e1f61d @ruan-brandao Update README.md
ruan-brandao authored Mar 31, 2014
599 =end
7e089fa @bbatsov Don't use block comments
authored May 1, 2013
600
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
601 # good
602 # comment line
603 # another comment line
604 ```
dc31232 initial version
Bozhidar Batsov authored Sep 12, 2011
605
606 ## Syntax
607
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
608 * <a name="double-colons"></a>
609 Use `::` only to reference constants(this includes classes and
610 modules) and constructors (like `Array()` or `Nokogiri::HTML()`).
343bf81 @bbatsov Replace "never" with "do not" here and there
authored Sep 17, 2014
611 Do not use `::` for regular method invocation.
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
612 <sup>[[link](#double-colons)]</sup>
f657893 @bbatsov Use :: only to reference constants
authored May 5, 2013
613
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
614 ```Ruby
615 # bad
616 SomeClass::some_method
617 some_object::some_method
f657893 @bbatsov Use :: only to reference constants
authored May 5, 2013
618
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
619 # good
620 SomeClass.some_method
621 some_object.some_method
622 SomeModule::SomeClass::SOME_CONST
623 SomeModule::SomeClass()
624 ```
f657893 @bbatsov Use :: only to reference constants
authored May 5, 2013
625
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
626 * <a name="method-parens"></a>
cf0d2be @sethkrasnianski Changed incorrect instances of 'arguments' to 'parameters'
sethkrasnianski authored Oct 26, 2014
627 Use `def` with parentheses when there are parameters. Omit the
628 parentheses when the method doesn't accept any parameters.
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
629 <sup>[[link](#method-parens)]</sup>
2681d4b added highlighting to all examples
Bozhidar Batsov authored Sep 13, 2011
630
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
631 ```Ruby
632 # bad
633 def some_method()
634 # body omitted
635 end
8ad4604 @mark-rushakoff Expand examples for parens and defining methods
mark-rushakoff authored May 29, 2013
636
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
637 # good
638 def some_method
639 # body omitted
640 end
dc31232 initial version
Bozhidar Batsov authored Sep 12, 2011
641
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
642 # bad
cf0d2be @sethkrasnianski Changed incorrect instances of 'arguments' to 'parameters'
sethkrasnianski authored Oct 26, 2014
643 def some_method_with_parameters param1, param2
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
644 # body omitted
645 end
8ad4604 @mark-rushakoff Expand examples for parens and defining methods
mark-rushakoff authored May 29, 2013
646
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
647 # good
cf0d2be @sethkrasnianski Changed incorrect instances of 'arguments' to 'parameters'
sethkrasnianski authored Oct 26, 2014
648 def some_method_with_parameters(param1, param2)
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
649 # body omitted
650 end
651 ```
dc31232 initial version
Bozhidar Batsov authored Sep 12, 2011
652
c0d1f32 @rrosenblum Add rule about defining optional arguments at the end of the argument…
rrosenblum authored Jul 25, 2015
653 * <a name="optional-arguments"></a>
654 Define optional arguments at the end of the list of arguments.
655 Ruby has some unexpected results when calling methods that have
656 optional arguments at the front of the list.
657 <sup>[[link](#optional-arguments)]</sup>
658
659 ```Ruby
660 # bad
661 def some_method(a = 1, b = 2, c, d)
662 puts "#{a}, #{b}, #{c}, #{d}"
663 end
664
665 some_method('w', 'x') # => '1, 2, w, x'
666 some_method('w', 'x', 'y') # => 'w, 2, x, y'
667 some_method('w', 'x', 'y', 'z') # => 'w, x, y, z'
668
669 # good
670 def some_method(c, d, a = 1, b = 2)
671 puts "#{a}, #{b}, #{c}, #{d}"
672 end
673
31b229b @araslanov-e Update README.md
araslanov-e authored Jan 19, 2016
674 some_method('w', 'x') # => '1, 2, w, x'
675 some_method('w', 'x', 'y') # => 'y, 2, w, x'
676 some_method('w', 'x', 'y', 'z') # => 'y, z, w, x'
c0d1f32 @rrosenblum Add rule about defining optional arguments at the end of the argument…
rrosenblum authored Jul 26, 2015
677 ```
678
3306992 @rrosenblum Add a section on parallel assignment
rrosenblum authored May 12, 2015
679 * <a name="parallel-assignment"></a>
680 Avoid the use of parallel assignment for defining variables. Parallel
681 assignment is allowed when it is the return of a method call, used with
682 the splat operator, or when used to swap variable assignment. Parallel
887ef0a @yous Remove mentioning parallel assignment is slow
yous authored Aug 13, 2015
683 assignment is less readable than separate assignment.
3306992 @rrosenblum Add a section on parallel assignment
rrosenblum authored May 12, 2015
684 <sup>[[link](#parallel-assignment)]</sup>
685
686 ```Ruby
687 # bad
688 a, b, c, d = 'foo', 'bar', 'baz', 'foobar'
689
690 # good
691 a = 'foo'
692 b = 'bar'
693 c = 'baz'
694 d = 'foobar'
695
696 # good - swapping variable assignment
697 # Swapping variable assignment is a special case because it will allow you to
698 # swap the values that are assigned to each variable.
699 a = 'foo'
700 b = 'bar'
701
702 a, b = b, a
703 puts a # => 'bar'
704 puts b # => 'foo'
705
706 # good - method return
707 def multi_return
708 [1, 2]
709 end
710
711 first, second = multi_return
712
713 # good - use with splat
c0e3a69 @accessd Show results of using the splat operator (#534)
accessd authored Apr 28, 2016
714 first, *list = [1, 2, 3, 4] # first => 1, list => [2, 3, 4]
3306992 @rrosenblum Add a section on parallel assignment
rrosenblum authored May 12, 2015
715
c0e3a69 @accessd Show results of using the splat operator (#534)
accessd authored Apr 28, 2016
716 hello_array = *'Hello' # => ["Hello"]
3306992 @rrosenblum Add a section on parallel assignment
rrosenblum authored May 12, 2015
717
c0e3a69 @accessd Show results of using the splat operator (#534)
accessd authored Apr 28, 2016
718 a = *(1..3) # => [1, 2, 3]
3306992 @rrosenblum Add a section on parallel assignment
rrosenblum authored May 12, 2015
719 ```
720
4487332 @rrosenblum Add a section about trailing underscore variables
rrosenblum authored Sep 22, 2015
721 * <a name="trailing-underscore-variables"></a>
722 Avoid the use of unnecessary trailing underscore variables during
49439ca @rrosenblum Make the example for trailing underscore variables clearer
rrosenblum authored Nov 19, 2015
723 parallel assignment. Named underscore variables are to be preferred over
724 underscore variables because of the context that they provide.
725 Trailing underscore variables are necessary when there is a splat variable
726 defined on the left side of the assignment, and the splat variable is
727 not an underscore.
4487332 @rrosenblum Add a section about trailing underscore variables
rrosenblum authored Sep 22, 2015
728 <sup>[[link]](#trailing-underscore-variables)</sup>
729
730 ```Ruby
731 # bad
49439ca @rrosenblum Make the example for trailing underscore variables clearer
rrosenblum authored Nov 19, 2015
732 foo = 'one,two,three,four,five'
733 # Unnecessary assignment that does not provide useful information
734 first, second, _ = foo.split(',')
735 first, _, _ = foo.split(',')
736 first, *_ = foo.split(',')
737
4487332 @rrosenblum Add a section about trailing underscore variables
rrosenblum authored Sep 22, 2015
738
739 # good
49439ca @rrosenblum Make the example for trailing underscore variables clearer
rrosenblum authored Nov 19, 2015
740 foo = 'one,two,three,four,five'
06d503f @richardtemple Update README.md (#562)
richardtemple authored Apr 22, 2016
741 # The underscores are needed to show that you want all elements
49439ca @rrosenblum Make the example for trailing underscore variables clearer
rrosenblum authored Nov 19, 2015
742 # except for the last number of underscore elements
743 *beginning, _ = foo.split(',')
744 *beginning, something, _ = foo.split(',')
745
746 a, = foo.split(',')
747 a, b, = foo.split(',')
748 # Unnecessary assignment to an unused variable, but the assignment
63a7c99 @cassioscabral Fix typo
cassioscabral authored Jan 21, 2016
749 # provides us with useful information.
49439ca @rrosenblum Make the example for trailing underscore variables clearer
rrosenblum authored Nov 19, 2015
750 first, _second = foo.split(',')
751 first, _second, = foo.split(',')
752 first, *_ending = foo.split(',')
4487332 @rrosenblum Add a section about trailing underscore variables
rrosenblum authored Sep 22, 2015
753 ```
754
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
755 * <a name="no-for-loops"></a>
343bf81 @bbatsov Replace "never" with "do not" here and there
authored Sep 17, 2014
756 Do not use `for`, unless you know exactly why. Most of the time iterators
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
757 should be used instead. `for` is implemented in terms of `each` (so
758 you're adding a level of indirection), but with a twist - `for`
759 doesn't introduce a new scope (unlike `each`) and variables defined
760 in its block will be visible outside it.
761 <sup>[[link](#no-for-loops)]</sup>
2681d4b added highlighting to all examples
Bozhidar Batsov authored Sep 13, 2011
762
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
763 ```Ruby
764 arr = [1, 2, 3]
8a50545 @mitio Remove trailing whitespace, fix punctuation and minor mistakes.
mitio authored Sep 17, 2011
765
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
766 # bad
767 for elem in arr do
768 puts elem
769 end
dc31232 initial version
Bozhidar Batsov authored Sep 12, 2011
770
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
771 # note that elem is accessible outside of the for loop
bba0447 @bruno- Update a couple inconsistent commented outputs
bruno- authored Jul 15, 2014
772 elem # => 3
b24b38d @bbatsov Show the difference in scoping between for and each (fixes #212)
authored Aug 16, 2013
773
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
774 # good
775 arr.each { |elem| puts elem }
b24b38d @bbatsov Show the difference in scoping between for and each (fixes #212)
authored Aug 16, 2013
776
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
777 # elem is not accessible outside each's block
bba0447 @bruno- Update a couple inconsistent commented outputs
bruno- authored Jul 15, 2014
778 elem # => NameError: undefined local variable or method `elem'
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
779 ```
8a50545 @mitio Remove trailing whitespace, fix punctuation and minor mistakes.
mitio authored Sep 17, 2011
780
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
781 * <a name="no-then"></a>
343bf81 @bbatsov Replace "never" with "do not" here and there
authored Sep 17, 2014
782 Do not use `then` for multi-line `if/unless`.
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
783 <sup>[[link](#no-then)]</sup>
dc31232 initial version
Bozhidar Batsov authored Sep 12, 2011
784
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
785 ```Ruby
786 # bad
787 if some_condition then
788 # body omitted
789 end
2681d4b added highlighting to all examples
Bozhidar Batsov authored Sep 13, 2011
790
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
791 # good
792 if some_condition
793 # body omitted
794 end
795 ```
dc31232 initial version
Bozhidar Batsov authored Sep 12, 2011
796
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
797 * <a name="same-line-condition"></a>
798 Always put the condition on the same line as the `if`/`unless` in a
799 multi-line conditional.
800 <sup>[[link](#same-line-condition)]</sup>
4483f80 @mark-rushakoff Put condition on same line as if/unless
mark-rushakoff authored Jun 4, 2013
801
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
802 ```Ruby
803 # bad
804 if
805 some_condition
806 do_something
807 do_something_else
808 end
4483f80 @mark-rushakoff Put condition on same line as if/unless
mark-rushakoff authored Jun 5, 2013
809
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
810 # good
811 if some_condition
812 do_something
813 do_something_else
814 end
815 ```
4483f80 @mark-rushakoff Put condition on same line as if/unless
mark-rushakoff authored Jun 5, 2013
816
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
817 * <a name="ternary-operator"></a>
818 Favor the ternary operator(`?:`) over `if/then/else/end` constructs.
5830b65 @ManuelArno favor ternary operator over if/then/else/end
ManuelArno authored Sep 18, 2011
819 It's more common and obviously more concise.
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
820 <sup>[[link](#ternary-operator)]</sup>
5e0a7a0 a few improvements
Bozhidar Batsov authored Sep 14, 2011
821
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
822 ```Ruby
823 # bad
824 result = if some_condition then something else something_else end
5e0a7a0 a few improvements
Bozhidar Batsov authored Sep 14, 2011
825
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
826 # good
827 result = some_condition ? something : something_else
828 ```
a08f5cc @bbatsov fixed a few errors
authored Sep 22, 2011
829
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
830 * <a name="no-nested-ternary"></a>
831 Use one expression per branch in a ternary operator. This
2405608 @ManuelArno add example to ternary operator rule
ManuelArno authored Sep 18, 2011
832 also means that ternary operators must not be nested. Prefer
6b1470c @bbatsov a few updates
authored Nov 23, 2011
833 `if/else` constructs in these cases.
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
834 <sup>[[link](#no-nested-ternary)]</sup>
2405608 @ManuelArno add example to ternary operator rule
ManuelArno authored Sep 18, 2011
835
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
836 ```Ruby
837 # bad
838 some_condition ? (nested_condition ? nested_something : nested_something_else) : something_else
2405608 @ManuelArno add example to ternary operator rule
ManuelArno authored Sep 18, 2011
839
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
840 # good
841 if some_condition
842 nested_condition ? nested_something : nested_something_else
843 else
844 something_else
845 end
846 ```
5e0a7a0 a few improvements
Bozhidar Batsov authored Sep 14, 2011
847
0db0c83 @bbatsov Remove an outdated rule
authored Sep 17, 2014
848 * <a name="no-semicolon-ifs"></a>
849 Do not use `if x; ...`. Use the ternary
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
850 operator instead.
0db0c83 @bbatsov Remove an outdated rule
authored Sep 17, 2014
851 <sup>[[link](#no-semicolon-ifs)]</sup>
ee895dd fixed the single line when rule and added a couple of new rules
Bozhidar Batsov authored Sep 16, 2011
852
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
853 ```Ruby
854 # bad
0db0c83 @bbatsov Remove an outdated rule
authored Sep 17, 2014
855 result = if some_condition; something else something_else end
ee895dd fixed the single line when rule and added a couple of new rules
Bozhidar Batsov authored Sep 16, 2011
856
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
857 # good
858 result = some_condition ? something : something_else
859 ```
ee895dd fixed the single line when rule and added a couple of new rules
Bozhidar Batsov authored Sep 16, 2011
860
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
861 * <a name="use-if-case-returns"></a>
862 Leverage the fact that `if` and `case` are expressions which return a
863 result.
864 <sup>[[link](#use-if-case-returns)]</sup>
f59f448 @bbatsov Leverage the fact that if and case are expressions
authored Feb 17, 2014
865
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
866 ```Ruby
867 # bad
868 if condition
869 result = x
870 else
871 result = y
872 end
873
874 # good
875 result =
f59f448 @bbatsov Leverage the fact that if and case are expressions
authored Feb 17, 2014
876 if condition
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
877 x
f59f448 @bbatsov Leverage the fact that if and case are expressions
authored Feb 17, 2014
878 else
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
879 y
f59f448 @bbatsov Leverage the fact that if and case are expressions
authored Feb 17, 2014
880 end
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
881 ```
f59f448 @bbatsov Leverage the fact that if and case are expressions
authored Feb 17, 2014
882
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
883 * <a name="one-line-cases"></a>
884 Use `when x then ...` for one-line cases. The alternative syntax `when x:
885 ...` has been removed as of Ruby 1.9.
886 <sup>[[link](#one-line-cases)]</sup>
ee895dd fixed the single line when rule and added a couple of new rules
Bozhidar Batsov authored Sep 16, 2011
887
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
888 * <a name="no-when-semicolons"></a>
343bf81 @bbatsov Replace "never" with "do not" here and there
authored Sep 17, 2014
889 Do not use `when x; ...`. See the previous rule.
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
890 <sup>[[link](#no-when-semicolons)]</sup>
1060e65 @bbatsov various updates
authored Sep 23, 2011
891
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
892 * <a name="bang-not-not"></a>
893 Use `!` instead of `not`.
894 <sup>[[link](#bang-not-not)]</sup>
9e1a308 @bbatsov Favor ! over not
authored May 8, 2013
895
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
896 ```Ruby
4f2bb66 @etdev Fix wording in #bang-not-not
etdev authored Nov 18, 2015
897 # bad - parentheses are required because of op precedence
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
898 x = (not something)
9e1a308 @bbatsov Favor ! over not
authored May 8, 2013
899
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
900 # good
901 x = !something
902 ```
9e1a308 @bbatsov Favor ! over not
authored May 8, 2013
903
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
904 * <a name="no-bang-bang"></a>
905 Avoid the use of `!!`.
906 <sup>[[link](#no-bang-bang)]</sup>
62192c1 @bbatsov [Fix #175] Add a rule about !!
authored Oct 11, 2013
907
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
908 ```Ruby
909 # bad
910 x = 'test'
911 # obscure nil check
912 if !!x
913 # body omitted
914 end
c252d36 @bbatsov Reword !! rule
authored Oct 11, 2013
915
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
916 x = false
917 # double negation is useless on booleans
918 !!x # => false
62192c1 @bbatsov [Fix #175] Add a rule about !!
authored Oct 11, 2013
919
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
920 # good
921 x = 'test'
922 unless x.nil?
923 # body omitted
924 end
925 ```
62192c1 @bbatsov [Fix #175] Add a rule about !!
authored Oct 11, 2013
926
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
927 * <a name="no-and-or-or"></a>
928 The `and` and `or` keywords are banned. It's just not worth it. Always use
929 `&&` and `||` instead.
930 <sup>[[link](#no-and-or-or)]</sup>
a5e10af added another example
Bozhidar Batsov authored Sep 14, 2011
931
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
932 ```Ruby
933 # bad
934 # boolean expression
935 if some_condition and some_other_condition
936 do_something
937 end
a5e10af added another example
Bozhidar Batsov authored Sep 14, 2011
938
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
939 # control flow
940 document.saved? or document.save!
5920497 @bbatsov Discourage the use of and/or
authored May 20, 2013
941
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
942 # good
943 # boolean expression
944 if some_condition && some_other_condition
945 do_something
946 end
5920497 @bbatsov Discourage the use of and/or
authored May 20, 2013
947
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
948 # control flow
949 document.saved? || document.save!
950 ```
a5e10af added another example
Bozhidar Batsov authored Sep 14, 2011
951
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
952 * <a name="no-multiline-ternary"></a>
953 Avoid multi-line `?:` (the ternary operator); use `if/unless` instead.
954 <sup>[[link](#no-multiline-ternary)]</sup>
7733e3e Added unless/else guideline
Burke Libbey authored Sep 19, 2011
955
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
956 * <a name="if-as-a-modifier"></a>
957 Favor modifier `if/unless` usage when you have a single-line body. Another
958 good alternative is the usage of control flow `&&/||`.
959 <sup>[[link](#if-as-a-modifier)]</sup>
dc31232 initial version
Bozhidar Batsov authored Sep 12, 2011
960
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
961 ```Ruby
962 # bad
963 if some_condition
964 do_something
965 end
dc31232 initial version
Bozhidar Batsov authored Sep 12, 2011
966
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
967 # good
968 do_something if some_condition
2681d4b added highlighting to all examples
Bozhidar Batsov authored Sep 13, 2011
969
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
970 # another good option
971 some_condition && do_something
972 ```
dc31232 initial version
Bozhidar Batsov authored Sep 12, 2011
973
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
974 * <a name="no-multiline-if-modifiers"></a>
975 Avoid modifier `if/unless` usage at the end of a non-trivial multi-line
976 block.
977 <sup>[[link](#no-multiline-if-modifiers)]</sup>
45d41b2 @aspiers Avoid if/unless at the end of a non-trivial block
aspiers authored Apr 14, 2014
978
979 ```Ruby
980 # bad
981 10.times do
982 # multi-line body omitted
983 end if some_condition
fff9233 @bbatsov Mention gittip
authored Apr 25, 2014
984
45d41b2 @aspiers Avoid if/unless at the end of a non-trivial block
aspiers authored Apr 14, 2014
985 # good
986 if some_condition
987 10.times do
988 # multi-line body omitted
989 end
990 end
991 ```
992
26760f8 @lumeet Add a section about nested modifiers
lumeet authored Oct 6, 2015
993 * <a name="no-nested-modifiers"></a>
994 Avoid nested modifier `if/unless/while/until` usage. Favor `&&/||` if
995 appropriate.
996 <sup>[[link](#no-nested-modifiers)]</sup>
997
998 ```Ruby
999 # bad
1000 do_something if other_condition if some_condition
1001
1002 # good
1003 do_something if some_condition && other_condition
1004 ```
1005
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
1006 * <a name="unless-for-negatives"></a>
1007 Favor `unless` over `if` for negative conditions (or control flow `||`).
1008 <sup>[[link](#unless-for-negatives)]</sup>
8a50545 @mitio Remove trailing whitespace, fix punctuation and minor mistakes.
mitio authored Sep 17, 2011
1009
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1010 ```Ruby
1011 # bad
1012 do_something if !some_condition
dc31232 initial version
Bozhidar Batsov authored Sep 12, 2011
1013
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1014 # bad
1015 do_something if not some_condition
c00d424 @lucianosousa Update README.md
lucianosousa authored May 17, 2013
1016
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1017 # good
1018 do_something unless some_condition
dc31232 initial version
Bozhidar Batsov authored Sep 12, 2011
1019
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1020 # another good option
1021 some_condition || do_something
1022 ```
a08f5cc @bbatsov fixed a few errors
authored Sep 22, 2011
1023
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
1024 * <a name="no-else-with-unless"></a>
343bf81 @bbatsov Replace "never" with "do not" here and there
authored Sep 17, 2014
1025 Do not use `unless` with `else`. Rewrite these with the positive case first.
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
1026 <sup>[[link](#no-else-with-unless)]</sup>
7733e3e Added unless/else guideline
Burke Libbey authored Sep 19, 2011
1027
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1028 ```Ruby
1029 # bad
1030 unless success?
1031 puts 'failure'
1032 else
1033 puts 'success'
1034 end
7733e3e Added unless/else guideline
Burke Libbey authored Sep 19, 2011
1035
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1036 # good
1037 if success?
1038 puts 'success'
1039 else
1040 puts 'failure'
1041 end
1042 ```
8a50545 @mitio Remove trailing whitespace, fix punctuation and minor mistakes.
mitio authored Sep 17, 2011
1043
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
1044 * <a name="no-parens-if"></a>
1045 Don't use parentheses around the condition of an `if/unless/while/until`.
1046 <sup>[[link](#no-parens-if)]</sup>
2f54da0 @bbatsov added a new rule about using parentheses around an if condition
authored Oct 11, 2011
1047
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1048 ```Ruby
1049 # bad
1050 if (x > 10)
1051 # body omitted
1052 end
2f54da0 @bbatsov added a new rule about using parentheses around an if condition
authored Oct 11, 2011
1053
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1054 # good
1055 if x > 10
1056 # body omitted
1057 end
1058 ```
2f54da0 @bbatsov added a new rule about using parentheses around an if condition
authored Oct 11, 2011
1059
6037975 note safe assignment exception to no parens around condition
Ted Lilley authored Aug 4, 2014
1060 Note that there is an exception to this rule, namely [safe assignment in
1061 condition](#safe-assignment-in-condition).
1062
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
1063 * <a name="no-multiline-while-do"></a>
343bf81 @bbatsov Replace "never" with "do not" here and there
authored Sep 17, 2014
1064 Do not use `while/until condition do` for multi-line `while/until`.
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
1065 <sup>[[link](#no-multiline-while-do)]</sup>
650662d @bbatsov Add a bit of advice about multi-line while/until
authored Jun 24, 2013
1066
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1067 ```Ruby
1068 # bad
1069 while x > 5 do
1070 # body omitted
1071 end
650662d @bbatsov Add a bit of advice about multi-line while/until
authored Jun 24, 2013
1072
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1073 until x > 5 do
1074 # body omitted
1075 end
650662d @bbatsov Add a bit of advice about multi-line while/until
authored Jun 24, 2013
1076
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1077 # good
1078 while x > 5
1079 # body omitted
1080 end
650662d @bbatsov Add a bit of advice about multi-line while/until
authored Jun 24, 2013
1081
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1082 until x > 5
1083 # body omitted
1084 end
1085 ```
650662d @bbatsov Add a bit of advice about multi-line while/until
authored Jun 24, 2013
1086
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
1087 * <a name="while-as-a-modifier"></a>
1088 Favor modifier `while/until` usage when you have a single-line body.
1089 <sup>[[link](#while-as-a-modifier)]</sup>
919473f @bbatsov added a nice quote by Russ Olsen on comments
authored Apr 20, 2012
1090
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1091 ```Ruby
1092 # bad
1093 while some_condition
1094 do_something
1095 end
919473f @bbatsov added a nice quote by Russ Olsen on comments
authored Apr 20, 2012
1096
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1097 # good
1098 do_something while some_condition
1099 ```
919473f @bbatsov added a nice quote by Russ Olsen on comments
authored Apr 20, 2012
1100
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
1101 * <a name="until-for-negatives"></a>
1102 Favor `until` over `while` for negative conditions.
1103 <sup>[[link](#until-for-negatives)]</sup>
919473f @bbatsov added a nice quote by Russ Olsen on comments
authored Apr 20, 2012
1104
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1105 ```Ruby
1106 # bad
1107 do_something while !some_condition
919473f @bbatsov added a nice quote by Russ Olsen on comments
authored Apr 20, 2012
1108
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1109 # good
1110 do_something until some_condition
1111 ```
919473f @bbatsov added a nice quote by Russ Olsen on comments
authored Apr 20, 2012
1112
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
1113 * <a name="infinite-loop"></a>
1114 Use `Kernel#loop` instead of `while/until` when you need an infinite loop.
1115 <sup>[[link](#infinite-loop)]</sup>
730a771 @bbatsov Use `Kernel#loop` instead of `while/until` when you need an infinite …
authored Apr 2, 2014
1116
1117 ```ruby
1118 # bad
1119 while true
1120 do_something
1121 end
1122
8cadfa6 Fix while/until typo
Wendel Scardua authored May 14, 2014
1123 until false
730a771 @bbatsov Use `Kernel#loop` instead of `while/until` when you need an infinite …
authored Apr 2, 2014
1124 do_something
1125 end
1126
1127 # good
1128 loop do
1129 do_something
1130 end
1131 ```
1132
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
1133 * <a name="loop-with-break"></a>
1134 Use `Kernel#loop` with `break` rather than `begin/end/until` or
1135 `begin/end/while` for post-loop tests.
1136 <sup>[[link](#loop-with-break)]</sup>
df933c0 @tgaff For post-loop tests use Kernel#loop over begin/end/while or
tgaff authored Mar 19, 2013
1137
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1138 ```Ruby
1139 # bad
1140 begin
1141 puts val
1142 val += 1
1143 end while val < 0
df933c0 @tgaff For post-loop tests use Kernel#loop over begin/end/while or
tgaff authored Mar 19, 2013
1144
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1145 # good
1146 loop do
1147 puts val
1148 val += 1
1149 break unless val < 0
1150 end
1151 ```
df933c0 @tgaff For post-loop tests use Kernel#loop over begin/end/while or
tgaff authored Mar 19, 2013
1152
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
1153 * <a name="no-dsl-parens"></a>
1154 Omit parentheses around parameters for methods that are part of an internal
1155 DSL (e.g. Rake, Rails, RSpec), methods that have "keyword" status in Ruby
1156 (e.g. `attr_reader`, `puts`) and attribute access methods. Use parentheses
1157 around the arguments of all other method invocations.
1158 <sup>[[link](#no-dsl-parens)]</sup>
8a50545 @mitio Remove trailing whitespace, fix punctuation and minor mistakes.
mitio authored Sep 17, 2011
1159
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1160 ```Ruby
1161 class Person
88bfea5 @Neodelf Improve some examples and apply some formatting corrections
Neodelf authored Mar 25, 2016
1162 # bad
1163 attr_reader(:name, :age)
1164 # good
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1165 attr_reader :name, :age
a08f5cc @bbatsov fixed a few errors
authored Sep 22, 2011
1166
88bfea5 @Neodelf Improve some examples and apply some formatting corrections
Neodelf authored Mar 25, 2016
1167 # body omitted
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1168 end
a08f5cc @bbatsov fixed a few errors
authored Sep 22, 2011
1169
88bfea5 @Neodelf Improve some examples and apply some formatting corrections
Neodelf authored Mar 25, 2016
1170 # bad
1171 temperance = Person.new 'Temperance', 30
1172 # good
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1173 temperance = Person.new('Temperance', 30)
a08f5cc @bbatsov fixed a few errors
authored Sep 22, 2011
1174
88bfea5 @Neodelf Improve some examples and apply some formatting corrections
Neodelf authored Mar 25, 2016
1175 # bad
1176 puts(temperance.age)
1177 # good
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1178 puts temperance.age
a08f5cc @bbatsov fixed a few errors
authored Sep 22, 2011
1179
88bfea5 @Neodelf Improve some examples and apply some formatting corrections
Neodelf authored Mar 25, 2016
1180 # bad
1181 x = Math.sin y
1182 # good
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1183 x = Math.sin(y)
88bfea5 @Neodelf Improve some examples and apply some formatting corrections
Neodelf authored Mar 25, 2016
1184
1185 # bad
1186 array.delete e
1187 # good
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1188 array.delete(e)
629a437 @ericridgeway Issue #156 - Add a small RSpec "no parenthesis" example
ericridgeway authored Mar 29, 2013
1189
88bfea5 @Neodelf Improve some examples and apply some formatting corrections
Neodelf authored Mar 25, 2016
1190 # bad
1191 expect(bowling.score).to eq 0
1192 # good
1193 expect(bowling.score).to eq(0)
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1194 ```
8a50545 @mitio Remove trailing whitespace, fix punctuation and minor mistakes.
mitio authored Sep 17, 2011
1195
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
1196 * <a name="no-braces-opts-hash"></a>
1197 Omit the outer braces around an implicit options hash.
1198 <sup>[[link](#no-braces-opts-hash)]</sup>
bb48cf3 @meagar [Fix #223] Cover use of {} in argument hash
meagar authored Sep 11, 2013
1199
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1200 ```Ruby
1201 # bad
1202 user.set({ name: 'John', age: 45, permissions: { read: true } })
bb48cf3 @meagar [Fix #223] Cover use of {} in argument hash
meagar authored Sep 11, 2013
1203
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1204 # good
1205 user.set(name: 'John', age: 45, permissions: { read: true })
1206 ```
bb48cf3 @meagar [Fix #223] Cover use of {} in argument hash
meagar authored Sep 11, 2013
1207
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
1208 * <a name="no-dsl-decorating"></a>
1209 Omit both the outer braces and parentheses for methods that are part of an
1210 internal DSL.
1211 <sup>[[link](#no-dsl-decorating)]</sup>
bb48cf3 @meagar [Fix #223] Cover use of {} in argument hash
meagar authored Sep 11, 2013
1212
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1213 ```Ruby
1214 class Person < ActiveRecord::Base
1215 # bad
1216 validates(:name, { presence: true, length: { within: 1..10 } })
bb48cf3 @meagar [Fix #223] Cover use of {} in argument hash
meagar authored Sep 11, 2013
1217
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1218 # good
1219 validates :name, presence: true, length: { within: 1..10 }
1220 end
1221 ```
bb48cf3 @meagar [Fix #223] Cover use of {} in argument hash
meagar authored Sep 11, 2013
1222
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
1223 * <a name="no-args-no-parens"></a>
1224 Omit parentheses for method calls with no arguments.
1225 <sup>[[link](#no-args-no-parens)]</sup>
12c7851 @bbatsov Add rule for method calls without args (fixes #145)
authored May 29, 2013
1226
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1227 ```Ruby
1228 # bad
1229 Kernel.exit!()
1230 2.even?()
1231 fork()
1232 'test'.upcase()
12c7851 @bbatsov Add rule for method calls without args (fixes #145)
authored May 29, 2013
1233
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1234 # good
1235 Kernel.exit!
1236 2.even?
1237 fork
1238 'test'.upcase
1239 ```
12c7851 @bbatsov Add rule for method calls without args (fixes #145)
authored May 29, 2013
1240
4364f00 @NewAlexandria add style for proc calling with simple blocks
NewAlexandria authored Mar 24, 2015
1241 * <a name="single-action-blocks"></a>
1242 Use the proc invocation shorthand when the invoked method is the only operation of a block.
1243 <sup>[[link](#single-action-blocks)]</sup>
1244
1245 ```Ruby
1246 # bad
1247 names.map { |name| name.upcase }
1248
1249 # good
1250 names.map(&:upcase)
1251 ```
1252
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
1253 * <a name="single-line-blocks"></a>
1254 Prefer `{...}` over `do...end` for single-line blocks. Avoid using `{...}`
c8e5ac8 @landongrindheim Hyphenate 'multi-line' consistently (#565)
landongrindheim authored Apr 27, 2016
1255 for multi-line blocks (multi-line chaining is always ugly). Always use
6ca9a1e @todb [Fix #319] Add anchors to rule bullets
todb authored Jul 18, 2014
1256 `do...end` for "control flow" and "method definitions" (e.g. in Rakefiles and
1257 certain DSLs). Avoid `do...end` when chaining.
1258 <sup>[[link](#single-line-blocks)]</sup>
931cf16 @bbatsov added an example
authored Oct 9, 2011
1259
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1260 ```Ruby
3a56cc4 @d-unseductable Fix inconsistency for an array of strings
d-unseductable authored Apr 27, 2015
1261 names = %w(Bozhidar Steve Sarah)
931cf16 @bbatsov added an example
authored Oct 9, 2011
1262
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1263 # bad
1264 names.each do |name|
1265 puts name
1266 end
931cf16 @bbatsov added an example
authored Oct 9, 2011
1267
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1268 # good
1269 names.each { |name| puts name }
931cf16 @bbatsov added an example
authored Oct 9, 2011
1270
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1271 # bad
1272 names.select do |name|
1273 name.start_with?('S')
1274 end.map { |name| name.upcase }
599839e Always place bad examples before good ones
Cecile Veneziani authored Apr 6, 2013
1275
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1276 # good
4364f00 @NewAlexandria add style for proc calling with simple blocks
NewAlexandria authored Mar 24, 2015
1277 names.select { |name| name.start_with?('S') }.map(&:upcase)
191384b @julio73 Style refactoring on indentation
julio73 authored Mar 5, 2014
1278 ```
931cf16 @bbatsov added an example
authored Oct 9, 2011
1279
c8e5ac8 @landongrindheim Hyphenate 'multi-line' consistently (#565)
landongrindheim authored Apr 27, 2016
1280 Some will argue that multi-line chaining would look OK with the use of {...},
89dc612 @yous Keep line length to 80
yous authored Jul 24, 2014
1281 but they should ask themselves - is this code really readable and can the
1282 blocks' contents be extracted into nifty methods?
dc31232 initial version