@@ -68,7 +68,6 @@ Translations of the guide are available in the following languages:
68
68
* [Chinese Simplified](https://github.com/JuanitoFatas/ruby-style-guide/blob/master/README-zhCN.md)
69
69
* [Chinese Traditional](https://github.com/JuanitoFatas/ruby-style-guide/blob/master/README-zhTW.md)
70
70
* [French](https://github.com/gauthier-delacroix/ruby-style-guide/blob/master/README-frFR.md)
71
- * [ German] ( https://github.com/arbox/de-ruby-style-guide/blob/master/README-deDE.md )
72
71
* [Japanese](https://github.com/fortissimo1997/ruby-style-guide/blob/japanese/README.ja.md)
73
72
* [Korean](https://github.com/dalzony/ruby-style-guide/blob/master/README-koKR.md)
74
73
* [Portuguese (pt-BR)](https://github.com/rubensmabueno/ruby-style-guide/blob/master/README-PT-BR.md)
@@ -217,7 +216,7 @@ Translations of the guide are available in the following languages:
217
216
class FooError < StandardError; end
218
217
```
219
218
220
- The only exception, regarding operators, is the exponent operator:
219
+ There are a few exceptions. One is the exponent operator:
221
220
222
221
```ruby
223
222
# bad
@@ -227,6 +226,16 @@ Translations of the guide are available in the following languages:
227
226
e = M * c**2
228
227
```
229
228
229
+ Another exception is the slash in rational literals:
230
+
231
+ ```ruby
232
+ # bad
233
+ o_scale = 1 / 48r
234
+
235
+ # good
236
+ o_scale = 1/48r
237
+ ```
238
+
230
239
* <a name="spaces-braces"></a>
231
240
No spaces after `(`, `[` or before `]`, `)`.
232
241
Use spaces around `{` and before `}`.
@@ -778,7 +787,7 @@ Translations of the guide are available in the following languages:
778
787
temperance = Person.new('Temperance', 30)
779
788
```
780
789
781
- Only omit parentheses for
790
+ Always omit parentheses for
782
791
783
792
* Method calls with no arguments:
784
793
@@ -816,13 +825,20 @@ Translations of the guide are available in the following languages:
816
825
817
826
# body omitted
818
827
end
828
+ ```
819
829
820
- # bad
821
- puts(temperance.age)
830
+ Can omit parentheses for
831
+
832
+ * Methods that have "keyword" status in Ruby, but are not declarative:
833
+
834
+ ```Ruby
822
835
# good
836
+ puts(temperance.age)
837
+ system('ls')
838
+ # also good
823
839
puts temperance.age
840
+ system 'ls'
824
841
```
825
-
826
842
* <a name="optional-arguments"></a>
827
843
Define optional arguments at the end of the list of arguments.
828
844
Ruby has some unexpected results when calling methods that have
@@ -1852,6 +1868,7 @@ no parameters.
1852
1868
1853
1869
* <a name="named-format-tokens"></a>
1854
1870
When using named format string tokens, favor `%<name>s` over `%{name}` because it encodes information about the type of the value.
1871
+ <sup>[[link]](#named-format-tokens)</sup>
1855
1872
1856
1873
```ruby
1857
1874
# bad
@@ -1860,7 +1877,6 @@ no parameters.
1860
1877
# good
1861
1878
format('Hello, %<name>s', name: 'John')
1862
1879
```
1863
- <sup>[[link](#named-format-tokens)</sup>
1864
1880
1865
1881
* <a name="array-join"></a>
1866
1882
Favor the use of `Array#join` over the fairly cryptic `Array#*` with
@@ -2626,6 +2642,42 @@ no parameters.
2626
2642
end
2627
2643
```
2628
2644
2645
+ * <a name="namespace-definition"></a>
2646
+ Define (and reopen) namespaced classes and modules using explicit nesting.
2647
+ Using the scope resolution operator can lead to surprising constant lookups
2648
+ due to Ruby's [lexical scoping](https://cirw.in/blog/constant-lookup.html),
2649
+ which depends on the module nesting at the point of definition.
2650
+ <sup>[[link](#namespace-definition)]</sup>
2651
+
2652
+ ```Ruby
2653
+ module Utilities
2654
+ class Queue
2655
+ end
2656
+ end
2657
+
2658
+ # bad
2659
+ class Utilities::Store
2660
+ Module.nesting # => [Utilities::Store]
2661
+
2662
+ def initialize
2663
+ # Refers to the top level ::Queue class because Utilities isn't in the
2664
+ # current nesting chain.
2665
+ @queue = Queue.new
2666
+ end
2667
+ end
2668
+
2669
+ # good
2670
+ module Utilities
2671
+ class WaitingList
2672
+ Module.nesting # => [Utilities::WaitingList, Utilities]
2673
+
2674
+ def initialize
2675
+ @queue = Queue.new # Refers to Utilities::Queue
2676
+ end
2677
+ end
2678
+ end
2679
+ ```
2680
+
2629
2681
* <a name="modules-vs-classes"></a>
2630
2682
Prefer modules to classes with only class methods. Classes should be used
2631
2683
only when it makes sense to create instances out of them.
@@ -3655,8 +3707,12 @@ resource cleanup when possible.
3655
3707
# bad
3656
3708
name = "Bozhidar"
3657
3709
3710
+ name = 'De\'Andre'
3711
+
3658
3712
# good
3659
3713
name = 'Bozhidar'
3714
+
3715
+ name = "De'Andre"
3660
3716
```
3661
3717
3662
3718
* **(Option B)** Prefer double-quotes unless your string literal
@@ -3666,8 +3722,12 @@ resource cleanup when possible.
3666
3722
# bad
3667
3723
name = 'Bozhidar'
3668
3724
3725
+ sarcasm = "I \"like\" it."
3726
+
3669
3727
# good
3670
3728
name = "Bozhidar"
3729
+
3730
+ sarcasm = 'I "like" it.'
3671
3731
```
3672
3732
3673
3733
The string literals in this guide are aligned with the first style.
@@ -3796,28 +3856,57 @@ resource cleanup when possible.
3796
3856
3797
3857
```ruby
3798
3858
# bad - using Powerpack String#strip_margin
3799
- code = << -END .strip_margin('|')
3859
+ code = <<-RUBY .strip_margin('|')
3800
3860
|def test
3801
3861
| some_method
3802
3862
| other_method
3803
3863
|end
3804
- END
3864
+ RUBY
3805
3865
3806
3866
# also bad
3807
- code = <<-END
3867
+ code = <<-RUBY
3808
3868
def test
3809
3869
some_method
3810
3870
other_method
3811
3871
end
3812
- END
3872
+ RUBY
3813
3873
3814
3874
# good
3815
- code = <<~END
3875
+ code = <<~RUBY
3816
3876
def test
3817
3877
some_method
3818
3878
other_method
3819
3879
end
3880
+ RUBY
3881
+ ```
3882
+
3883
+ * <a name="heredoc-delimiters"></a>
3884
+ Use descriptive delimiters for heredocs. Delimiters add valuable information
3885
+ about the heredoc content, and as an added bonus some editors can highlight
3886
+ code within heredocs if the correct delimiter is used.
3887
+ <sup>[[link](#heredoc-delimiters)]</sup>
3888
+
3889
+ ```ruby
3890
+ # bad
3891
+ code = <<~END
3892
+ def foo
3893
+ bar
3894
+ end
3820
3895
END
3896
+
3897
+ # good
3898
+ code = <<~RUBY
3899
+ def foo
3900
+ bar
3901
+ end
3902
+ RUBY
3903
+
3904
+ # good
3905
+ code = <<~SUMMARY
3906
+ An imposing black structure provides a connection between the past and
3907
+ the future in this enigmatic adaptation of a short story by revered
3908
+ sci-fi author Arthur C. Clarke.
3909
+ SUMMARY
3821
3910
```
3822
3911
3823
3912
## Date & Time
@@ -4134,7 +4223,7 @@ resource cleanup when possible.
4134
4223
4135
4224
```ruby
4136
4225
# bad
4137
- def method_missing? (meth, * params, & block)
4226
+ def method_missing(meth, *params, &block)
4138
4227
if /^find_by_(?<prop>.*)/ =~ meth
4139
4228
# ... lots of code to do a find_by
4140
4229
else
@@ -4143,7 +4232,7 @@ resource cleanup when possible.
4143
4232
end
4144
4233
4145
4234
# good
4146
- def method_missing? (meth, * params, & block)
4235
+ def method_missing(meth, *params, &block)
4147
4236
if /^find_by_(?<prop>.*)/ =~ meth
4148
4237
find_by(prop, *params, &block)
4149
4238
else
0 commit comments