-
Notifications
You must be signed in to change notification settings - Fork 3
/
file.rubocop_ruby_style_guide.html
7195 lines (6742 loc) · 449 KB
/
file.rubocop_ruby_style_guide.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0, user-scalable=no'>
<meta name='apple-touch-fullscreen' content='yes'>
<meta name='apple-mobile-web-app-capable' content='yes'>
<meta name='apple-mobile-web-app-status-bar-style' content='rgba(228,228,228,1.0)'>
<title>RuboCop Ruby Style Guide</title>
<link rel='stylesheet' type='text/css' href='css/y_fonts.css' />
<link rel='stylesheet' type='text/css' href='css/highlight.github.css' />
<link rel='stylesheet' type='text/css' href='css/y_style.css' />
<link rel='stylesheet' type='text/css' href='css/y_list.css' />
<link rel='stylesheet' type='text/css' href='css/y_color.css' />
<script type='text/javascript'>
var pathId = "rubocop_ruby_style_guide",
relpath = '';
var t2Info = {
CSEP: '.',
ISEP: '#',
NSEP: '::'
};
</script>
<script type='text/javascript' charset='utf-8' src='js/highlight.pack.js'></script>
<script type='text/javascript' charset='utf-8' src='js/y_app.js'></script>
</head>
<body>
<svg id='y_wait' class viewBox='0 0 90 90'></svg>
<div id='settings' class='hidden'></div>
<div id='y_list' class='d h'>
<header id='list_header'></header>
<nav id= 'list_nav' class='y_nav l_nav'>
<ul id='list_items'></ul>
</nav>
</div>
<div id='y_toc' class='f h'>
<header id='toc_header'></header>
<nav id= 'toc_nav' class='y_nav t_nav'>
<ol id='toc_items'></ol>
</nav>
</div>
<div id='y_main' tabindex='-1'>
<header id='y_header'>
<div id='y_menu'>
<a id='home_no_xhr' href='/'>Home</a> »
<a href='.'>RuboStyle</a> »
<a href='_index.html'>Index</a> »
<span class='title'><a id='t2_doc_top' href='#'>RuboCop Ruby Style Guide ▲</a></span>
</div>
<a id='list_href' href="file_list.html"></a>
<div id='y_measure_em' class='y_measure'></div>
<div id='y_measure_vh' class='y_measure'></div>
<span id='y_measure_50pre' class='y_measure'><code>123456789_123456789_123456789_123456789_123456789_</code></span>
</header>
<div id='content' class='file'>
<h1>RuboCop Ruby Style Guide - <span style='font-size: 1rem'>Rev: 2022-08-24 58f7c09</span></h1>
<ul style='columns: 3;'>
<li><a href='#introduction'>Introduction</a></li>
<li><a href='#source-code-layout'>Source Code Layout</a></li>
<li><a href='#naming-conventions'>Naming Conventions</a></li>
<li><a href='#flow-of-control'>Flow of Control</a></li>
<li><a href='#exceptions'>Exceptions</a></li>
<li><a href='#assignment-comparison'>Assignment & Comparison</a></li>
<li><a href='#blocks-procs-lambdas'>Blocks, Procs & Lambdas</a></li>
<li><a href='#methods'>Methods</a></li>
<li><a href='#classes-modules'>Classes & Modules</a></li>
<li><a href='#classes-constructors'>Classes: Constructors</a></li>
<li><a href='#comments'>Comments</a></li>
<li><a href='#comment-annotations'>Comment Annotations</a></li>
<li><a href='#magic-comments'>Magic Comments</a></li>
<li><a href='#collections'>Collections</a></li>
<li><a href='#numbers'>Numbers</a></li>
<li><a href='#strings'>Strings</a></li>
<li><a href='#heredocs'>Heredocs</a></li>
<li><a href='#date-time'>Date & Time</a></li>
<li><a href='#regular-expressions'>Regular Expressions</a></li>
<li><a href='#percent-literals'>Percent Literals</a></li>
<li><a href='#metaprogramming'>Metaprogramming</a></li>
<li><a href='#api-documentation'>API Documentation</a></li>
<li><a href='#gemfile-and-gemspec'>Gemfile and Gemspec</a></li>
<li><a href='#misc'>Misc</a></li>
<li><a href='#tools'>Tools</a></li>
<li><a href='#history'>History</a></li>
<li><a href='#sources-of-inspiration'>Sources of Inspiration</a></li>
<li><a href='#contributing'>Contributing</a></li>
<li><a href='#colophon'>Colophon</a></li>
<li><a href='#license'>License</a></li>
<li><a href='#spread-the-word'>Spread the Word</a></li>
</ul>
<div class="sect1">
<h2 id="introduction">Introduction</h2>
<div class="sectionbody">
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>Role models are important.</p>
</div>
</blockquote>
<div class="attribution">
— Officer Alex J. Murphy / RoboCop
</div>
</div>
<div class="paragraph">
<p>This Ruby style guide recommends best practices so that real-world Ruby programmers can write code that can be maintained by other real-world Ruby programmers.
A style guide that reflects real-world usage gets used, while a style guide that holds to an ideal that has been rejected by the people it is supposed to help risks not getting used at all - no matter how good it is.</p>
</div>
<div class="paragraph">
<p>The guide is separated into several sections of related guidelines.
We’ve tried to add the rationale behind the guidelines (if it’s omitted we’ve assumed it’s pretty obvious).</p>
</div>
<div class="paragraph">
<p>We didn’t come up with all the guidelines out of nowhere - they are mostly based on the professional experience of the editors, feedback and suggestions from members of the Ruby community and various highly regarded Ruby programming resources, such as <a href="https://pragprog.com/book/ruby4/programming-ruby-1-9-2-0">"Programming Ruby"</a> and <a href="https://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177">"The Ruby Programming Language"</a>.</p>
</div>
<div class="paragraph">
<p>This style guide evolves over time as additional conventions are identified and past conventions are rendered obsolete by changes in Ruby itself.</p>
</div>
<div class="admonitionblock tip">
<table>
<tr>
<td class="icon">
<div class="title">Tip</div>
</td>
<td class="content">
<div class="paragraph">
<p>If you’re into Rails or RSpec you might want to check out the complementary <a href="https://github.com/rubocop/rails-style-guide">Ruby on Rails Style Guide</a> and <a href="https://github.com/rubocop/rspec-style-guide">RSpec Style Guide</a>.</p>
</div>
</td>
</tr>
</table>
</div>
<div class="admonitionblock tip">
<table>
<tr>
<td class="icon">
<div class="title">Tip</div>
</td>
<td class="content">
<a href="https://github.com/rubocop/rubocop">RuboCop</a> is a static code analyzer (linter) and formatter, based on this style guide.
</td>
</tr>
</table>
</div>
<div class="sect2">
<h3 id="guiding-principles">Guiding Principles</h3>
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>Programs must be written for people to read, and only incidentally for machines to execute.</p>
</div>
</blockquote>
<div class="attribution">
— Harold Abelson<br>
<cite>Structure and Interpretation of Computer Programs</cite>
</div>
</div>
<div class="paragraph">
<p>It’s common knowledge that code is read much more often than it is written.
The guidelines provided here are intended to improve the readability of code and make it consistent across the wide spectrum of Ruby code.
They are also meant to reflect real-world usage of Ruby instead of a random ideal. When we had to choose between a very established practice
and a subjectively better alternative we’ve opted to recommend the established practice.<sup class="footnote">[<a id="_footnoteref_1" class="footnote" href="#_footnotedef_1" title="View footnote.">1</a>]</sup></p>
</div>
<div class="paragraph">
<p>There are some areas in which there is no clear consensus in the Ruby community regarding a particular style (like string literal quoting, spacing inside hash literals, dot position in multi-line method chaining, etc.).
In such scenarios all popular styles are acknowledged and it’s up to you to pick one and apply it consistently.</p>
</div>
<div class="paragraph">
<p>Ruby had existed for over 15 years by the time
the guide was created, and the language’s flexibility and lack of common standards have contributed to the
creation of numerous styles for just about everything. Rallying people around the cause of community standards
took a lot of time and energy, and we still have a lot of ground to cover.</p>
</div>
<div class="paragraph">
<p>Ruby is famously optimized for programmer happiness. We’d like to believe that this guide is going to help you optimize for maximum
programmer happiness.</p>
</div>
</div>
<div class="sect2">
<h3 id="a-note-about-consistency">A Note about Consistency</h3>
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>A foolish consistency is the hobgoblin of little minds, adored by little statesmen and philosophers and divines.</p>
</div>
</blockquote>
<div class="attribution">
— Ralph Waldo Emerson
</div>
</div>
<div class="paragraph">
<p>A style guide is about consistency.
Consistency with this style guide is important.
Consistency within a project is more important.
Consistency within one class or method is the most important.</p>
</div>
<div class="paragraph">
<p>However, know when to be inconsistent — sometimes style guide recommendations just aren’t applicable. When in doubt, use your best judgment.
Look at other examples and decide what looks best. And don’t hesitate to ask!</p>
</div>
<div class="paragraph">
<p>In particular: do not break backwards compatibility just to comply with this guide!</p>
</div>
<div class="paragraph">
<p>Some other good reasons to ignore a particular guideline:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>When applying the guideline would make the code less readable, even for someone who is used to reading code that follows this style guide.</p>
</li>
<li>
<p>To be consistent with surrounding code that also breaks it (maybe for historic reasons) — although this is also an opportunity to clean up someone else’s mess (in true XP style).</p>
</li>
<li>
<p>Because the code in question predates the introduction of the guideline and there is no other reason to be modifying that code.</p>
</li>
<li>
<p>When the code needs to remain compatible with older versions of Ruby that don’t support the feature recommended by the style guide.</p>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="translations">Translations</h3>
<div class="paragraph">
<p>Translations of the guide are available in the following languages:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/JuanitoFatas/ruby-style-guide/blob/master/README-zhCN.md">Chinese Simplified</a></p>
</li>
<li>
<p><a href="https://github.com/JuanitoFatas/ruby-style-guide/blob/master/README-zhTW.md">Chinese Traditional</a></p>
</li>
<li>
<p><a href="https://github.com/HassanTC/ruby-style-guide/blob/master/README-EgAr.md">Egyptian Arabic</a></p>
</li>
<li>
<p><a href="https://github.com/gauthier-delacroix/ruby-style-guide/blob/master/README-frFR.md">French</a></p>
</li>
<li>
<p><a href="https://github.com/fortissimo1997/ruby-style-guide/blob/japanese/README.ja.md">Japanese</a></p>
</li>
<li>
<p><a href="https://github.com/dalzony/ruby-style-guide/blob/master/README-koKR.md">Korean</a></p>
</li>
<li>
<p><a href="https://github.com/rubensmabueno/ruby-style-guide/blob/master/README-PT-BR.md">Portuguese (pt-BR)</a></p>
</li>
<li>
<p><a href="https://github.com/arbox/ruby-style-guide/blob/master/README-ruRU.md">Russian</a></p>
</li>
<li>
<p><a href="https://github.com/alemohamad/ruby-style-guide/blob/master/README-esLA.md">Spanish</a></p>
</li>
<li>
<p><a href="https://github.com/CQBinh/ruby-style-guide/blob/master/README-viVN.md">Vietnamese</a></p>
</li>
</ul>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<div class="title">Note</div>
</td>
<td class="content">
These translations are not maintained by our editor team, so their quality
and level of completeness may vary. The translated versions of the guide often
lag behind the upstream English version.
</td>
</tr>
</table>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="source-code-layout">Source Code Layout</h2>
<div class="sectionbody">
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>Nearly everybody is convinced that every style but their own is
ugly and unreadable. Leave out the "but their own" and they’re
probably right…​</p>
</div>
</blockquote>
<div class="attribution">
— Jerry Coffin (on indentation)
</div>
</div>
<div class="sect2">
<h3 id="utf-8">Source Encoding</h3>
<div class="paragraph">
<p>Use <code>UTF-8</code> as the source file encoding.</p>
</div>
<div class="admonitionblock tip">
<table>
<tr>
<td class="icon">
<div class="title">Tip</div>
</td>
<td class="content">
UTF-8 has been the default source file encoding since Ruby 2.0.
</td>
</tr>
</table>
</div>
</div>
<div class="sect2">
<h3 id="tabs-or-spaces">Tabs or Spaces?</h3>
<div class="paragraph">
<p>Use only spaces for indentation. No hard tabs.</p>
</div>
</div>
<div class="sect2">
<h3 id="spaces-indentation">Indentation</h3>
<div class="paragraph">
<p>Use two <strong>spaces</strong> per indentation level (aka soft tabs).</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="code ruby"><code class="ruby"><span class='comment'># bad - four spaces
</span><span class='kw'>def</span> <span class='id identifier rubyid_some_method'>some_method</span>
<span class='id identifier rubyid_do_something'>do_something</span>
<span class='kw'>end</span>
<span class='comment'># good
</span><span class='kw'>def</span> <span class='id identifier rubyid_some_method'>some_method</span>
<span class='id identifier rubyid_do_something'>do_something</span>
<span class='kw'>end</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="max-line-length">Maximum Line Length</h3>
<div class="paragraph">
<p>Limit lines to 80 characters.</p>
</div>
<div class="admonitionblock tip">
<table>
<tr>
<td class="icon">
<div class="title">Tip</div>
</td>
<td class="content">
Most editors and IDEs have configuration options to help you with that.
They would typically highlight lines that exceed the length limit.
</td>
</tr>
</table>
</div>
<div class="sidebarblock">
<div class="content">
<div class="title">Why Bother with 80 characters in a World of Modern Widescreen Displays?</div>
<div class="paragraph">
<p>A lot of people these days feel that a maximum line length of 80 characters is
just a remnant of the past and makes little sense today. After all - modern
displays can easily fit 200+ characters on a single line. Still, there are some
important benefits to be gained from sticking to shorter lines of code.</p>
</div>
<div class="paragraph">
<p>First, and foremost - numerous studies have shown that humans read much faster
vertically and very long lines of text impede the reading process. As noted
earlier, one of the guiding principles of this style guide is to optimize the
code we write for human consumption.</p>
</div>
<div class="paragraph">
<p>Additionally, limiting the required editor window width makes it possible to
have several files open side-by-side, and works well when using code review
tools that present the two versions in adjacent columns.</p>
</div>
<div class="paragraph">
<p>The default wrapping in most tools disrupts the visual structure of the code,
making it more difficult to understand. The limits are chosen to avoid wrapping
in editors with the window width set to 80, even if the tool places a marker
glyph in the final column when wrapping lines. Some web based tools may not
offer dynamic line wrapping at all.</p>
</div>
<div class="paragraph">
<p>Some teams strongly prefer a longer line length. For code maintained exclusively
or primarily by a team that can reach agreement on this issue, it is okay to
increase the line length limit up to 100 characters, or all the way up
to 120 characters. Please, restrain the urge to go beyond 120 characters.</p>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="no-trailing-whitespace">No Trailing Whitespace</h3>
<div class="paragraph">
<p>Avoid trailing whitespace.</p>
</div>
<div class="admonitionblock tip">
<table>
<tr>
<td class="icon">
<div class="title">Tip</div>
</td>
<td class="content">
Most editors and IDEs have configuration options to visualize trailing whitespace and
to remove it automatically on save.
</td>
</tr>
</table>
</div>
</div>
<div class="sect2">
<h3 id="crlf">Line Endings</h3>
<div class="paragraph">
<p>Use Unix-style line endings.<sup class="footnote">[<a id="_footnoteref_2" class="footnote" href="#_footnotedef_2" title="View footnote.">2</a>]</sup></p>
</div>
<div class="admonitionblock tip">
<table>
<tr>
<td class="icon">
<div class="title">Tip</div>
</td>
<td class="content">
<div class="paragraph">
<p>If you’re using Git you might want to add the following configuration setting to protect your project from Windows line endings creeping in:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-bash" data-lang="bash">$ git config --global core.autocrlf true</code></pre>
</div>
</div>
</td>
</tr>
</table>
</div>
</div>
<div class="sect2">
<h3 id="newline-eof">Should I Terminate Files with a Newline?</h3>
<div class="paragraph">
<p>End each file with a newline.</p>
</div>
<div class="admonitionblock tip">
<table>
<tr>
<td class="icon">
<div class="title">Tip</div>
</td>
<td class="content">
This should be done via editor configuration, not manually.
</td>
</tr>
</table>
</div>
</div>
<div class="sect2">
<h3 id="no-semicolon">Should I Terminate Expressions with <code>;</code>?</h3>
<div class="paragraph">
<p>Don’t use <code>;</code> to terminate statements and expressions.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="code ruby"><code class="ruby"><span class='comment'># bad
</span><span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>foobar</span><span class='tstring_end'>'</span></span><span class='semicolon'>;</span> <span class='comment'># superfluous semicolon
</span>
<span class='comment'># good
</span><span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>foobar</span><span class='tstring_end'>'</span></span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="one-expression-per-line">One Expression Per Line</h3>
<div class="paragraph">
<p>Use one expression per line.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="code ruby"><code class="ruby"><span class='comment'># bad
</span><span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>foo</span><span class='tstring_end'>'</span></span><span class='semicolon'>;</span> <span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>bar</span><span class='tstring_end'>'</span></span> <span class='comment'># two expressions on the same line
</span>
<span class='comment'># good
</span><span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>foo</span><span class='tstring_end'>'</span></span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>bar</span><span class='tstring_end'>'</span></span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>foo</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>bar</span><span class='tstring_end'>'</span></span> <span class='comment'># this applies to puts in particular</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="spaces-operators">Spaces and Operators</h3>
<div class="paragraph">
<p>Use spaces around operators, after commas, colons and semicolons.
Whitespace might be (mostly) irrelevant to the Ruby interpreter, but its proper use is the key to writing easily readable code.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="code ruby"><code class="ruby"><span class='comment'># bad
</span><span class='id identifier rubyid_sum'>sum</span><span class='op'>=</span><span class='int'>1</span><span class='op'>+</span><span class='int'>2</span>
<span class='id identifier rubyid_a'>a</span><span class='comma'>,</span><span class='id identifier rubyid_b'>b</span><span class='op'>=</span><span class='int'>1</span><span class='comma'>,</span><span class='int'>2</span>
<span class='kw'>class</span> <span class='const'>FooError</span><span class='op'><</span><span class='const'>StandardError</span><span class='semicolon'>;</span><span class='kw'>end</span>
<span class='comment'># good
</span><span class='id identifier rubyid_sum'>sum</span> <span class='op'>=</span> <span class='int'>1</span> <span class='op'>+</span> <span class='int'>2</span>
<span class='id identifier rubyid_a'>a</span><span class='comma'>,</span> <span class='id identifier rubyid_b'>b</span> <span class='op'>=</span> <span class='int'>1</span><span class='comma'>,</span> <span class='int'>2</span>
<span class='kw'>class</span> <span class='const'>FooError</span> <span class='op'><</span> <span class='const'>StandardError</span><span class='semicolon'>;</span> <span class='kw'>end</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>There are a few exceptions:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Exponent operator:</p>
</li>
</ul>
</div>
<div class="listingblock">
<div class="content">
<pre class="code ruby"><code class="ruby"><span class='comment'># bad
</span><span class='id identifier rubyid_e'>e</span> <span class='op'>=</span> <span class='const'>M</span> <span class='op'>*</span> <span class='id identifier rubyid_c'>c</span> <span class='op'>**</span> <span class='int'>2</span>
<span class='comment'># good
</span><span class='id identifier rubyid_e'>e</span> <span class='op'>=</span> <span class='const'>M</span> <span class='op'>*</span> <span class='id identifier rubyid_c'>c</span><span class='op'>**</span><span class='int'>2</span></code></pre>
</div>
</div>
<div class="ulist">
<ul>
<li>
<p>Slash in rational literals:</p>
</li>
</ul>
</div>
<div class="listingblock">
<div class="content">
<pre class="code ruby"><code class="ruby"><span class='comment'># bad
</span><span class='id identifier rubyid_o_scale'>o_scale</span> <span class='op'>=</span> <span class='int'>1</span> <span class='op'>/</span> <span class='rational'>48r</span>
<span class='comment'># good
</span><span class='id identifier rubyid_o_scale'>o_scale</span> <span class='op'>=</span> <span class='int'>1</span><span class='op'>/</span><span class='rational'>48r</span></code></pre>
</div>
</div>
<div class="ulist">
<ul>
<li>
<p>Safe navigation operator:</p>
</li>
</ul>
</div>
<div class="listingblock">
<div class="content">
<pre class="code ruby"><code class="ruby"><span class='comment'># bad
</span><span class='id identifier rubyid_foo'>foo</span> <span class='op'>&.</span> <span class='id identifier rubyid_bar'>bar</span>
<span class='id identifier rubyid_foo'>foo</span> <span class='op'>&.</span><span class='id identifier rubyid_bar'>bar</span>
<span class='id identifier rubyid_foo'>foo</span><span class='op'>&.</span> <span class='id identifier rubyid_bar'>bar</span>
<span class='comment'># good
</span><span class='id identifier rubyid_foo'>foo</span><span class='op'>&.</span><span class='id identifier rubyid_bar'>bar</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="spaces-braces">Spaces and Braces</h3>
<div class="paragraph">
<p>No spaces after <code>(</code>, <code>[</code> or before <code>]</code>, <code>)</code>.
Use spaces around <code>{</code> and before <code>}</code>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="code ruby"><code class="ruby"><span class='comment'># bad
</span><span class='id identifier rubyid_some'>some</span>( <span class='id identifier rubyid_arg'>arg</span> ).<span class='id identifier rubyid_other'>other</span>
[ <span class='int'>1</span><span class='comma'>,</span> <span class='int'>2</span><span class='comma'>,</span> <span class='int'>3</span> ].<span class='id identifier rubyid_each'>each</span>{<span class='op'>|</span><span class='id identifier rubyid_e'>e</span><span class='op'>|</span> <span class='id identifier rubyid_puts'>puts</span> <span class='id identifier rubyid_e'>e</span>}
<span class='comment'># good
</span><span class='id identifier rubyid_some'>some</span>(<span class='id identifier rubyid_arg'>arg</span>).<span class='id identifier rubyid_other'>other</span>
[<span class='int'>1</span><span class='comma'>,</span> <span class='int'>2</span><span class='comma'>,</span> <span class='int'>3</span>].<span class='id identifier rubyid_each'>each</span> { <span class='op'>|</span><span class='id identifier rubyid_e'>e</span><span class='op'>|</span> <span class='id identifier rubyid_puts'>puts</span> <span class='id identifier rubyid_e'>e</span> }</code></pre>
</div>
</div>
<div class="paragraph">
<p><code>{</code> and <code>}</code> deserve a bit of clarification, since they are used for block and hash literals, as well as string interpolation.</p>
</div>
<div class="paragraph">
<p>For hash literals two styles are considered acceptable.
The first variant is slightly more readable (and arguably more popular in the Ruby community in general).
The second variant has the advantage of adding visual difference between block and hash literals.
Whichever one you pick - apply it consistently.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="code ruby"><code class="ruby"><span class='comment'># good - space after { and before }
</span>{ <span class='label'>one:</span> <span class='int'>1</span><span class='comma'>,</span> <span class='label'>two:</span> <span class='int'>2</span> }
<span class='comment'># good - no space after { and before }
</span>{<span class='label'>one:</span> <span class='int'>1</span><span class='comma'>,</span> <span class='label'>two:</span> <span class='int'>2</span>}</code></pre>
</div>
</div>
<div class="paragraph">
<p>With interpolated expressions, there should be no padded-spacing inside the braces.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="code ruby"><code class="ruby"><span class='comment'># bad
</span><span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>From: </span><span class='embexpr_beg'>#{</span> <span class='id identifier rubyid_user'>user</span>.<span class='id identifier rubyid_first_name'>first_name</span> <span class='embexpr_end'>}</span><span class='tstring_content'>, </span><span class='embexpr_beg'>#{</span> <span class='id identifier rubyid_user'>user</span>.<span class='id identifier rubyid_last_name'>last_name</span> <span class='embexpr_end'>}</span><span class='tstring_end'>"</span></span>
<span class='comment'># good
</span><span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>From: </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_user'>user</span>.<span class='id identifier rubyid_first_name'>first_name</span><span class='embexpr_end'>}</span><span class='tstring_content'>, </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_user'>user</span>.<span class='id identifier rubyid_last_name'>last_name</span><span class='embexpr_end'>}</span><span class='tstring_end'>"</span></span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="no-space-bang">No Space after Bang</h3>
<div class="paragraph">
<p>No space after <code>!</code>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="code ruby"><code class="ruby"><span class='comment'># bad
</span><span class='op'>!</span> <span class='id identifier rubyid_something'>something</span>
<span class='comment'># good
</span><span class='op'>!</span><span class='id identifier rubyid_something'>something</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="no-space-inside-range-literals">No Space inside Range Literals</h3>
<div class="paragraph">
<p>No space inside range literals.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="code ruby"><code class="ruby"><span class='comment'># bad
</span><span class='int'>1</span> <span class='op'>..</span> <span class='int'>3</span>
<span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>a</span><span class='tstring_end'>'</span></span> <span class='op'>...</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>z</span><span class='tstring_end'>'</span></span>
<span class='comment'># good
</span><span class='int'>1</span><span class='op'>..</span><span class='int'>3</span>
<span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>a</span><span class='tstring_end'>'</span></span><span class='op'>...</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>z</span><span class='tstring_end'>'</span></span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="indent-when-to-case">Indent <code>when</code> to <code>case</code></h3>
<div class="paragraph">
<p>Indent <code>when</code> as deep as <code>case</code>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="code ruby"><code class="ruby"><span class='comment'># bad
</span><span class='kw'>case</span>
<span class='kw'>when</span> <span class='id identifier rubyid_song'>song</span>.<span class='id identifier rubyid_name'>name</span> <span class='op'>==</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>Misty</span><span class='tstring_end'>'</span></span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>Not again!</span><span class='tstring_end'>'</span></span>
<span class='kw'>when</span> <span class='id identifier rubyid_song'>song</span>.<span class='id identifier rubyid_duration'>duration</span> <span class='op'>></span> <span class='int'>120</span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>Too long!</span><span class='tstring_end'>'</span></span>
<span class='kw'>when</span> <span class='const'>Time</span>.<span class='id identifier rubyid_now'>now</span>.<span class='id identifier rubyid_hour'>hour</span> <span class='op'>></span> <span class='int'>21</span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>It's too late</span><span class='tstring_end'>"</span></span>
<span class='kw'>else</span>
<span class='id identifier rubyid_song'>song</span>.<span class='id identifier rubyid_play'>play</span>
<span class='kw'>end</span>
<span class='comment'># good
</span><span class='kw'>case</span>
<span class='kw'>when</span> <span class='id identifier rubyid_song'>song</span>.<span class='id identifier rubyid_name'>name</span> <span class='op'>==</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>Misty</span><span class='tstring_end'>'</span></span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>Not again!</span><span class='tstring_end'>'</span></span>
<span class='kw'>when</span> <span class='id identifier rubyid_song'>song</span>.<span class='id identifier rubyid_duration'>duration</span> <span class='op'>></span> <span class='int'>120</span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>Too long!</span><span class='tstring_end'>'</span></span>
<span class='kw'>when</span> <span class='const'>Time</span>.<span class='id identifier rubyid_now'>now</span>.<span class='id identifier rubyid_hour'>hour</span> <span class='op'>></span> <span class='int'>21</span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>It's too late</span><span class='tstring_end'>"</span></span>
<span class='kw'>else</span>
<span class='id identifier rubyid_song'>song</span>.<span class='id identifier rubyid_play'>play</span>
<span class='kw'>end</span></code></pre>
</div>
</div>
<div class="sidebarblock">
<div class="content">
<div class="title">A Bit of History</div>
<div class="paragraph">
<p>This is the style established in both "The Ruby Programming Language" and "Programming Ruby".
Historically it is derived from the fact that <code>case</code> and <code>switch</code> statements are not blocks, hence should not be indented, and the <code>when</code> and <code>else</code> keywords are labels (compiled in the C language, they are literally labels for <code>JMP</code> calls).</p>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="indent-conditional-assignment">Indent Conditional Assignment</h3>
<div class="paragraph">
<p>When assigning the result of a conditional expression to a variable, preserve the usual alignment of its branches.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="code ruby"><code class="ruby"><span class='comment'># bad - pretty convoluted
</span><span class='id identifier rubyid_kind'>kind</span> <span class='op'>=</span> <span class='kw'>case</span> <span class='id identifier rubyid_year'>year</span>
<span class='kw'>when</span> <span class='int'>1850</span><span class='op'>..</span><span class='int'>1889</span> <span class='kw'>then</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>Blues</span><span class='tstring_end'>'</span></span>
<span class='kw'>when</span> <span class='int'>1890</span><span class='op'>..</span><span class='int'>1909</span> <span class='kw'>then</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>Ragtime</span><span class='tstring_end'>'</span></span>
<span class='kw'>when</span> <span class='int'>1910</span><span class='op'>..</span><span class='int'>1929</span> <span class='kw'>then</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>New Orleans Jazz</span><span class='tstring_end'>'</span></span>
<span class='kw'>when</span> <span class='int'>1930</span><span class='op'>..</span><span class='int'>1939</span> <span class='kw'>then</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>Swing</span><span class='tstring_end'>'</span></span>
<span class='kw'>when</span> <span class='int'>1940</span><span class='op'>..</span><span class='int'>1950</span> <span class='kw'>then</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>Bebop</span><span class='tstring_end'>'</span></span>
<span class='kw'>else</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>Jazz</span><span class='tstring_end'>'</span></span>
<span class='kw'>end</span>
<span class='id identifier rubyid_result'>result</span> <span class='op'>=</span> <span class='kw'>if</span> <span class='id identifier rubyid_some_cond'>some_cond</span>
<span class='id identifier rubyid_calc_something'>calc_something</span>
<span class='kw'>else</span>
<span class='id identifier rubyid_calc_something_else'>calc_something_else</span>
<span class='kw'>end</span>
<span class='comment'># good - it's apparent what's going on
</span><span class='id identifier rubyid_kind'>kind</span> <span class='op'>=</span> <span class='kw'>case</span> <span class='id identifier rubyid_year'>year</span>
<span class='kw'>when</span> <span class='int'>1850</span><span class='op'>..</span><span class='int'>1889</span> <span class='kw'>then</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>Blues</span><span class='tstring_end'>'</span></span>
<span class='kw'>when</span> <span class='int'>1890</span><span class='op'>..</span><span class='int'>1909</span> <span class='kw'>then</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>Ragtime</span><span class='tstring_end'>'</span></span>
<span class='kw'>when</span> <span class='int'>1910</span><span class='op'>..</span><span class='int'>1929</span> <span class='kw'>then</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>New Orleans Jazz</span><span class='tstring_end'>'</span></span>
<span class='kw'>when</span> <span class='int'>1930</span><span class='op'>..</span><span class='int'>1939</span> <span class='kw'>then</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>Swing</span><span class='tstring_end'>'</span></span>
<span class='kw'>when</span> <span class='int'>1940</span><span class='op'>..</span><span class='int'>1950</span> <span class='kw'>then</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>Bebop</span><span class='tstring_end'>'</span></span>
<span class='kw'>else</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>Jazz</span><span class='tstring_end'>'</span></span>
<span class='kw'>end</span>
<span class='id identifier rubyid_result'>result</span> <span class='op'>=</span> <span class='kw'>if</span> <span class='id identifier rubyid_some_cond'>some_cond</span>
<span class='id identifier rubyid_calc_something'>calc_something</span>
<span class='kw'>else</span>
<span class='id identifier rubyid_calc_something_else'>calc_something_else</span>
<span class='kw'>end</span>
<span class='comment'># good (and a bit more width efficient)
</span><span class='id identifier rubyid_kind'>kind</span> <span class='op'>=</span>
<span class='kw'>case</span> <span class='id identifier rubyid_year'>year</span>
<span class='kw'>when</span> <span class='int'>1850</span><span class='op'>..</span><span class='int'>1889</span> <span class='kw'>then</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>Blues</span><span class='tstring_end'>'</span></span>
<span class='kw'>when</span> <span class='int'>1890</span><span class='op'>..</span><span class='int'>1909</span> <span class='kw'>then</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>Ragtime</span><span class='tstring_end'>'</span></span>
<span class='kw'>when</span> <span class='int'>1910</span><span class='op'>..</span><span class='int'>1929</span> <span class='kw'>then</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>New Orleans Jazz</span><span class='tstring_end'>'</span></span>
<span class='kw'>when</span> <span class='int'>1930</span><span class='op'>..</span><span class='int'>1939</span> <span class='kw'>then</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>Swing</span><span class='tstring_end'>'</span></span>
<span class='kw'>when</span> <span class='int'>1940</span><span class='op'>..</span><span class='int'>1950</span> <span class='kw'>then</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>Bebop</span><span class='tstring_end'>'</span></span>
<span class='kw'>else</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>Jazz</span><span class='tstring_end'>'</span></span>
<span class='kw'>end</span>
<span class='id identifier rubyid_result'>result</span> <span class='op'>=</span>
<span class='kw'>if</span> <span class='id identifier rubyid_some_cond'>some_cond</span>
<span class='id identifier rubyid_calc_something'>calc_something</span>
<span class='kw'>else</span>
<span class='id identifier rubyid_calc_something_else'>calc_something_else</span>
<span class='kw'>end</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="empty-lines-between-methods">Empty Lines between Methods</h3>
<div class="paragraph">
<p>Use empty lines between method definitions and also to break up methods into logical paragraphs internally.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="code ruby"><code class="ruby"><span class='comment'># bad
</span><span class='kw'>def</span> <span class='id identifier rubyid_some_method'>some_method</span>
<span class='id identifier rubyid_data'>data</span> <span class='op'>=</span> <span class='id identifier rubyid_initialize'>initialize</span>(<span class='id identifier rubyid_options'>options</span>)
<span class='id identifier rubyid_data'>data</span>.<span class='id identifier rubyid_manipulate!'>manipulate!</span>
<span class='id identifier rubyid_data'>data</span>.<span class='id identifier rubyid_result'>result</span>
<span class='kw'>end</span>
<span class='kw'>def</span> <span class='id identifier rubyid_some_other_method'>some_other_method</span>
<span class='id identifier rubyid_result'>result</span>
<span class='kw'>end</span>
<span class='comment'># good
</span><span class='kw'>def</span> <span class='id identifier rubyid_some_method'>some_method</span>
<span class='id identifier rubyid_data'>data</span> <span class='op'>=</span> <span class='id identifier rubyid_initialize'>initialize</span>(<span class='id identifier rubyid_options'>options</span>)
<span class='id identifier rubyid_data'>data</span>.<span class='id identifier rubyid_manipulate!'>manipulate!</span>
<span class='id identifier rubyid_data'>data</span>.<span class='id identifier rubyid_result'>result</span>
<span class='kw'>end</span>
<span class='kw'>def</span> <span class='id identifier rubyid_some_other_method'>some_other_method</span>
<span class='id identifier rubyid_result'>result</span>
<span class='kw'>end</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="two-or-more-empty-lines">Two or More Empty Lines</h3>
<div class="paragraph">
<p>Don’t use several empty lines in a row.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="code ruby"><code class="ruby"><span class='comment'># bad - It has two empty lines.
</span><span class='id identifier rubyid_some_method'>some_method</span>
<span class='id identifier rubyid_some_method'>some_method</span>
<span class='comment'># good
</span><span class='id identifier rubyid_some_method'>some_method</span>
<span class='id identifier rubyid_some_method'>some_method</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="empty-lines-around-attribute-accessor">Empty Lines around Attribute Accessor</h3>
<div class="paragraph">
<p>Use empty lines around attribute accessor.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="code ruby"><code class="ruby"><span class='comment'># bad
</span><span class='kw'>class</span> <span class='const'>Foo</span>
<span class='id identifier rubyid_attr_reader'>attr_reader</span> <span class='symbeg'>:</span><span class='id identifier rubyid_foo'>foo</span>
<span class='kw'>def</span> <span class='id identifier rubyid_foo'>foo</span>
<span class='comment'># do something...
</span> <span class='kw'>end</span>
<span class='kw'>end</span>
<span class='comment'># good
</span><span class='kw'>class</span> <span class='const'>Foo</span>
<span class='id identifier rubyid_attr_reader'>attr_reader</span> <span class='symbeg'>:</span><span class='id identifier rubyid_foo'>foo</span>
<span class='kw'>def</span> <span class='id identifier rubyid_foo'>foo</span>
<span class='comment'># do something...
</span> <span class='kw'>end</span>
<span class='kw'>end</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="empty-lines-around-access-modifier">Empty Lines around Access Modifier</h3>
<div class="paragraph">
<p>Use empty lines around access modifier.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="code ruby"><code class="ruby"><span class='comment'># bad
</span><span class='kw'>class</span> <span class='const'>Foo</span>
<span class='kw'>def</span> <span class='id identifier rubyid_bar'>bar</span><span class='semicolon'>;</span> <span class='kw'>end</span>
<span class='id identifier rubyid_private'>private</span>
<span class='kw'>def</span> <span class='id identifier rubyid_baz'>baz</span><span class='semicolon'>;</span> <span class='kw'>end</span>
<span class='kw'>end</span>
<span class='comment'># good
</span><span class='kw'>class</span> <span class='const'>Foo</span>
<span class='kw'>def</span> <span class='id identifier rubyid_bar'>bar</span><span class='semicolon'>;</span> <span class='kw'>end</span>
<span class='id identifier rubyid_private'>private</span>
<span class='kw'>def</span> <span class='id identifier rubyid_baz'>baz</span><span class='semicolon'>;</span> <span class='kw'>end</span>
<span class='kw'>end</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="empty-lines-around-bodies">Empty Lines around Bodies</h3>
<div class="paragraph">
<p>Don’t use empty lines around method, class, module, block bodies.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="code ruby"><code class="ruby"><span class='comment'># bad
</span><span class='kw'>class</span> <span class='const'>Foo</span>
<span class='kw'>def</span> <span class='id identifier rubyid_foo'>foo</span>
<span class='kw'>begin</span>
<span class='id identifier rubyid_do_something'>do_something</span> <span class='kw'>do</span>
<span class='id identifier rubyid_something'>something</span>
<span class='kw'>end</span>
<span class='kw'>rescue</span>
<span class='id identifier rubyid_something'>something</span>
<span class='kw'>end</span>
<span class='kw'>true</span>
<span class='kw'>end</span>
<span class='kw'>end</span>
<span class='comment'># good
</span><span class='kw'>class</span> <span class='const'>Foo</span>
<span class='kw'>def</span> <span class='id identifier rubyid_foo'>foo</span>
<span class='kw'>begin</span>
<span class='id identifier rubyid_do_something'>do_something</span> <span class='kw'>do</span>
<span class='id identifier rubyid_something'>something</span>
<span class='kw'>end</span>
<span class='kw'>rescue</span>
<span class='id identifier rubyid_something'>something</span>
<span class='kw'>end</span>
<span class='kw'>end</span>
<span class='kw'>end</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="no-trailing-params-comma">Trailing Comma in Method Arguments</h3>
<div class="paragraph">
<p>Avoid comma after the last parameter in a method call, especially when the parameters are not on separate lines.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="code ruby"><code class="ruby"><span class='comment'># bad - easier to move/add/remove parameters, but still not preferred
</span><span class='id identifier rubyid_some_method'>some_method</span>(
<span class='id identifier rubyid_size'>size</span><span class='comma'>,</span>
<span class='id identifier rubyid_count'>count</span><span class='comma'>,</span>
<span class='id identifier rubyid_color'>color</span><span class='comma'>,</span>
)
<span class='comment'># bad
</span><span class='id identifier rubyid_some_method'>some_method</span>(<span class='id identifier rubyid_size'>size</span><span class='comma'>,</span> <span class='id identifier rubyid_count'>count</span><span class='comma'>,</span> <span class='id identifier rubyid_color'>color</span><span class='comma'>,</span> )
<span class='comment'># good
</span><span class='id identifier rubyid_some_method'>some_method</span>(<span class='id identifier rubyid_size'>size</span><span class='comma'>,</span> <span class='id identifier rubyid_count'>count</span><span class='comma'>,</span> <span class='id identifier rubyid_color'>color</span>)</code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="spaces-around-equals">Spaces around Equals</h3>
<div class="paragraph">
<p>Use spaces around the <code>=</code> operator when assigning default values to method parameters:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="code ruby"><code class="ruby"><span class='comment'># bad
</span><span class='kw'>def</span> <span class='id identifier rubyid_some_method'>some_method</span>(<span class='id identifier rubyid_arg1'>arg1</span><span class='op'>=</span><span class='symbeg'>:</span><span class='id identifier rubyid_default'>default</span><span class='comma'>,</span> <span class='id identifier rubyid_arg2'>arg2</span><span class='op'>=</span><span class='kw'>nil</span><span class='comma'>,</span> <span class='id identifier rubyid_arg3'>arg3</span><span class='op'>=</span>[])
<span class='comment'># do something...
</span><span class='kw'>end</span>
<span class='comment'># good
</span><span class='kw'>def</span> <span class='id identifier rubyid_some_method'>some_method</span>(<span class='id identifier rubyid_arg1'>arg1</span> <span class='op'>=</span> <span class='symbeg'>:</span><span class='id identifier rubyid_default'>default</span><span class='comma'>,</span> <span class='id identifier rubyid_arg2'>arg2</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='comma'>,</span> <span class='id identifier rubyid_arg3'>arg3</span> <span class='op'>=</span> [])
<span class='comment'># do something...
</span><span class='kw'>end</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>While several Ruby books suggest the first style, the second is much more
prominent in practice (and arguably a bit more readable).</p>
</div>
</div>
<div class="sect2">
<h3 id="no-trailing-backslash">Line Continuation in Expressions</h3>
<div class="paragraph">
<p>Avoid line continuation with <code>\</code> where not required.
In practice, avoid using line continuations for anything but string concatenation.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="code ruby"><code class="ruby"><span class='comment'># bad (\ is not needed here)
</span><span class='id identifier rubyid_result'>result</span> <span class='op'>=</span> <span class='int'>1</span> <span class='op'>-</span> \
<span class='int'>2</span>
<span class='comment'># bad (\ is required, but still ugly as hell)
</span><span class='id identifier rubyid_result'>result</span> <span class='op'>=</span> <span class='int'>1</span> \
<span class='op'>-</span> <span class='int'>2</span>
<span class='comment'># good
</span><span class='id identifier rubyid_result'>result</span> <span class='op'>=</span> <span class='int'>1</span> <span class='op'>-</span>
<span class='int'>2</span>
<span class='id identifier rubyid_long_string'>long_string</span> <span class='op'>=</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>First part of the long string</span><span class='tstring_end'>'</span></span> \
<span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'> and second part of the long string</span><span class='tstring_end'>'</span></span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="consistent-multi-line-chains">Multi-line Method Chains</h3>
<div class="paragraph">
<p>Adopt a consistent multi-line method chaining style.
There are two popular styles in the Ruby community, both of which are considered good - leading <code>.</code> and trailing <code>.</code>.</p>
</div>
<div class="sect3">
<h4 id="leading-dot-in-multi-line-chains">Leading <code>.</code></h4>
<div class="paragraph">
<p>When continuing a chained method call on another line, keep the <code>.</code> on the second line.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="code ruby"><code class="ruby"><span class='comment'># bad - need to consult first line to understand second line
</span><span class='id identifier rubyid_one'>one</span>.<span class='id identifier rubyid_two'>two</span>.<span class='id identifier rubyid_three'>three</span>.
<span class='id identifier rubyid_four'>four</span>
<span class='comment'># good - it's immediately clear what's going on the second line
</span><span class='id identifier rubyid_one'>one</span>.<span class='id identifier rubyid_two'>two</span>.<span class='id identifier rubyid_three'>three</span>
.<span class='id identifier rubyid_four'>four</span></code></pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="trailing-dot-in-multi-line-chains">Trailing <code>.</code></h4>
<div class="paragraph">
<p>When continuing a chained method call on another line, include the <code>.</code> on the first line to indicate that the expression continues.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="code ruby"><code class="ruby"><span class='comment'># bad - need to read ahead to the second line to know that the chain continues
</span><span class='id identifier rubyid_one'>one</span>.<span class='id identifier rubyid_two'>two</span>.<span class='id identifier rubyid_three'>three</span>
.<span class='id identifier rubyid_four'>four</span>