rubocop-standard 4.1.0 → 5.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/{LICENSE → LICENSE.txt} +0 -0
- data/README.md +8 -8
- data/config/default.yml +12 -310
- data/config/rails.yml +0 -108
- metadata +19 -45
- data/STYLEGUIDE.md +0 -763
- data/guides/rails-controller-render-shorthand.md +0 -9
- data/guides/rails-render-inline.md +0 -27
- data/guides/rails-render-literal.md +0 -8
- data/lib/rubocop/cop/standard/rails.rb +0 -10
- data/lib/rubocop/cop/standard/rails/rails_application_record.rb +0 -27
- data/lib/rubocop/cop/standard/rails/rails_controller_render_action_symbol.rb +0 -43
- data/lib/rubocop/cop/standard/rails/rails_controller_render_paths_exist.rb +0 -63
- data/lib/rubocop/cop/standard/rails/rails_controller_render_shorthand.rb +0 -51
- data/lib/rubocop/cop/standard/rails/rails_render_inline.rb +0 -27
- data/lib/rubocop/cop/standard/rails/rails_render_object_collection.rb +0 -45
- data/lib/rubocop/cop/standard/rails/rails_view_render_paths_exist.rb +0 -55
- data/lib/rubocop/cop/standard/rails/rails_view_render_shorthand.rb +0 -38
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubocop-standard
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 5.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Garen Torikian
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-12-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rubocop
|
@@ -67,77 +67,51 @@ dependencies:
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: rubocop-rake
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - "
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '5.0'
|
76
|
-
type: :development
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - "~>"
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '5.0'
|
83
|
-
- !ruby/object:Gem::Dependency
|
84
|
-
name: minitest
|
85
|
-
requirement: !ruby/object:Gem::Requirement
|
86
|
-
requirements:
|
87
|
-
- - "~>"
|
73
|
+
- - ">="
|
88
74
|
- !ruby/object:Gem::Version
|
89
|
-
version: '
|
90
|
-
type: :
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
91
77
|
prerelease: false
|
92
78
|
version_requirements: !ruby/object:Gem::Requirement
|
93
79
|
requirements:
|
94
|
-
- - "
|
80
|
+
- - ">="
|
95
81
|
- !ruby/object:Gem::Version
|
96
|
-
version: '
|
82
|
+
version: '0'
|
97
83
|
- !ruby/object:Gem::Dependency
|
98
84
|
name: rake
|
99
85
|
requirement: !ruby/object:Gem::Requirement
|
100
86
|
requirements:
|
101
|
-
- - "
|
87
|
+
- - ">="
|
102
88
|
- !ruby/object:Gem::Version
|
103
|
-
version: '
|
89
|
+
version: '0'
|
104
90
|
type: :development
|
105
91
|
prerelease: false
|
106
92
|
version_requirements: !ruby/object:Gem::Requirement
|
107
93
|
requirements:
|
108
|
-
- - "
|
94
|
+
- - ">="
|
109
95
|
- !ruby/object:Gem::Version
|
110
|
-
version: '
|
111
|
-
description:
|
96
|
+
version: '0'
|
97
|
+
description: Enables all the RuboCop recommendations (with the overly aggressive ones
|
98
|
+
disabled).
|
112
99
|
email:
|
113
100
|
- gjtorikian@gmail.com
|
114
101
|
executables: []
|
115
102
|
extensions: []
|
116
103
|
extra_rdoc_files: []
|
117
104
|
files:
|
118
|
-
- LICENSE
|
105
|
+
- LICENSE.txt
|
119
106
|
- README.md
|
120
|
-
- STYLEGUIDE.md
|
121
107
|
- config/default.yml
|
122
108
|
- config/minitest.yml
|
123
109
|
- config/rails.yml
|
124
|
-
- guides/rails-controller-render-shorthand.md
|
125
|
-
- guides/rails-render-inline.md
|
126
|
-
- guides/rails-render-literal.md
|
127
|
-
- lib/rubocop/cop/standard/rails.rb
|
128
|
-
- lib/rubocop/cop/standard/rails/rails_application_record.rb
|
129
|
-
- lib/rubocop/cop/standard/rails/rails_controller_render_action_symbol.rb
|
130
|
-
- lib/rubocop/cop/standard/rails/rails_controller_render_paths_exist.rb
|
131
|
-
- lib/rubocop/cop/standard/rails/rails_controller_render_shorthand.rb
|
132
|
-
- lib/rubocop/cop/standard/rails/rails_render_inline.rb
|
133
|
-
- lib/rubocop/cop/standard/rails/rails_render_object_collection.rb
|
134
|
-
- lib/rubocop/cop/standard/rails/rails_view_render_paths_exist.rb
|
135
|
-
- lib/rubocop/cop/standard/rails/rails_view_render_shorthand.rb
|
136
110
|
homepage: https://github.com/gjtorikian/rubocop-standard
|
137
111
|
licenses:
|
138
112
|
- MIT
|
139
113
|
metadata: {}
|
140
|
-
post_install_message:
|
114
|
+
post_install_message:
|
141
115
|
rdoc_options: []
|
142
116
|
require_paths:
|
143
117
|
- lib
|
@@ -152,8 +126,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
152
126
|
- !ruby/object:Gem::Version
|
153
127
|
version: '0'
|
154
128
|
requirements: []
|
155
|
-
rubygems_version: 3.1.
|
156
|
-
signing_key:
|
129
|
+
rubygems_version: 3.1.4
|
130
|
+
signing_key:
|
157
131
|
specification_version: 4
|
158
132
|
summary: RuboCop Standard
|
159
133
|
test_files: []
|
data/STYLEGUIDE.md
DELETED
@@ -1,763 +0,0 @@
|
|
1
|
-
# Ruby Style Guide
|
2
|
-
|
3
|
-
* Use soft-tabs with a two space indent.
|
4
|
-
|
5
|
-
* Keep each line of code to a readable length. Unless you have a reason to, keep lines to fewer than 100 characters.
|
6
|
-
|
7
|
-
* Never leave trailing whitespace.
|
8
|
-
|
9
|
-
* End each file with a [newline](https://github.com/bbatsov/ruby-style-guide#newline-eof).
|
10
|
-
|
11
|
-
* Use spaces around operators, after commas, colons and semicolons, around `{`
|
12
|
-
and before `}`.
|
13
|
-
|
14
|
-
``` ruby
|
15
|
-
sum = 1 + 2
|
16
|
-
a, b = 1, 2
|
17
|
-
1 > 2 ? true : false; puts "Hi"
|
18
|
-
[1, 2, 3].each { |e| puts e }
|
19
|
-
```
|
20
|
-
|
21
|
-
* No spaces after `(`, `[` or before `]`, `)`.
|
22
|
-
|
23
|
-
``` ruby
|
24
|
-
some(arg).other
|
25
|
-
[1, 2, 3].length
|
26
|
-
```
|
27
|
-
|
28
|
-
* No spaces after `!`.
|
29
|
-
|
30
|
-
``` ruby
|
31
|
-
!array.include?(element)
|
32
|
-
```
|
33
|
-
|
34
|
-
* Indent `when` as deep as `case`.
|
35
|
-
|
36
|
-
``` ruby
|
37
|
-
case
|
38
|
-
when song.name == "Misty"
|
39
|
-
puts "Not again!"
|
40
|
-
when song.duration > 120
|
41
|
-
puts "Too long!"
|
42
|
-
when Time.now.hour > 21
|
43
|
-
puts "It's too late"
|
44
|
-
else
|
45
|
-
song.play
|
46
|
-
end
|
47
|
-
|
48
|
-
kind = case year
|
49
|
-
when 1850..1889 then "Blues"
|
50
|
-
when 1890..1909 then "Ragtime"
|
51
|
-
when 1910..1929 then "New Orleans Jazz"
|
52
|
-
when 1930..1939 then "Swing"
|
53
|
-
when 1940..1950 then "Bebop"
|
54
|
-
else "Jazz"
|
55
|
-
end
|
56
|
-
```
|
57
|
-
|
58
|
-
* Use empty lines between `def`s and to break up a method into logical
|
59
|
-
paragraphs.
|
60
|
-
|
61
|
-
``` ruby
|
62
|
-
def some_method
|
63
|
-
data = initialize(options)
|
64
|
-
|
65
|
-
data.manipulate!
|
66
|
-
|
67
|
-
data.result
|
68
|
-
end
|
69
|
-
|
70
|
-
def some_method
|
71
|
-
result
|
72
|
-
end
|
73
|
-
```
|
74
|
-
|
75
|
-
## Classes
|
76
|
-
|
77
|
-
* Avoid the usage of class (`@@`) variables due to their unusual behavior
|
78
|
-
in inheritance.
|
79
|
-
|
80
|
-
``` ruby
|
81
|
-
class Parent
|
82
|
-
@@class_var = "parent"
|
83
|
-
|
84
|
-
def self.print_class_var
|
85
|
-
puts @@class_var
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
class Child < Parent
|
90
|
-
@@class_var = "child"
|
91
|
-
end
|
92
|
-
|
93
|
-
Parent.print_class_var # => will print "child"
|
94
|
-
```
|
95
|
-
|
96
|
-
As you can see all the classes in a class hierarchy actually share one
|
97
|
-
class variable. Class instance variables should usually be preferred
|
98
|
-
over class variables.
|
99
|
-
|
100
|
-
* Use `def self.method` to define singleton methods. This makes the methods
|
101
|
-
more resistant to refactoring changes.
|
102
|
-
|
103
|
-
``` ruby
|
104
|
-
class TestClass
|
105
|
-
# bad
|
106
|
-
def TestClass.some_method
|
107
|
-
# body omitted
|
108
|
-
end
|
109
|
-
|
110
|
-
# good
|
111
|
-
def self.some_other_method
|
112
|
-
# body omitted
|
113
|
-
end
|
114
|
-
```
|
115
|
-
|
116
|
-
* Avoid `class << self` except when necessary, e.g. single accessors and aliased
|
117
|
-
attributes.
|
118
|
-
|
119
|
-
``` ruby
|
120
|
-
class TestClass
|
121
|
-
# bad
|
122
|
-
class << self
|
123
|
-
def first_method
|
124
|
-
# body omitted
|
125
|
-
end
|
126
|
-
|
127
|
-
def second_method_etc
|
128
|
-
# body omitted
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
# good
|
133
|
-
class << self
|
134
|
-
attr_accessor :per_page
|
135
|
-
alias_method :nwo, :find_by_name_with_owner
|
136
|
-
end
|
137
|
-
|
138
|
-
def self.first_method
|
139
|
-
# body omitted
|
140
|
-
end
|
141
|
-
|
142
|
-
def self.second_method_etc
|
143
|
-
# body omitted
|
144
|
-
end
|
145
|
-
end
|
146
|
-
```
|
147
|
-
|
148
|
-
* Indent the `public`, `protected`, and `private` methods as much the
|
149
|
-
method definitions they apply to. Leave one blank line above them.
|
150
|
-
|
151
|
-
``` ruby
|
152
|
-
class SomeClass
|
153
|
-
def public_method
|
154
|
-
# ...
|
155
|
-
end
|
156
|
-
|
157
|
-
private
|
158
|
-
def private_method
|
159
|
-
# ...
|
160
|
-
end
|
161
|
-
end
|
162
|
-
```
|
163
|
-
|
164
|
-
* Avoid explicit use of `self` as the recipient of internal class or instance
|
165
|
-
messages unless to specify a method shadowed by a variable.
|
166
|
-
|
167
|
-
``` ruby
|
168
|
-
class SomeClass
|
169
|
-
attr_accessor :message
|
170
|
-
|
171
|
-
def greeting(name)
|
172
|
-
message = "Hi #{name}" # local variable in Ruby, not attribute writer
|
173
|
-
self.message = message
|
174
|
-
end
|
175
|
-
end
|
176
|
-
```
|
177
|
-
|
178
|
-
## Collections
|
179
|
-
|
180
|
-
* Prefer `%w` to the literal array syntax when you need an array of
|
181
|
-
strings.
|
182
|
-
|
183
|
-
``` ruby
|
184
|
-
# bad
|
185
|
-
STATES = ["draft", "open", "closed"]
|
186
|
-
|
187
|
-
# good
|
188
|
-
STATES = %w(draft open closed)
|
189
|
-
```
|
190
|
-
|
191
|
-
* Use `Set` instead of `Array` when dealing with unique elements. `Set`
|
192
|
-
implements a collection of unordered values with no duplicates. This
|
193
|
-
is a hybrid of `Array`'s intuitive inter-operation facilities and
|
194
|
-
`Hash`'s fast lookup.
|
195
|
-
|
196
|
-
* Use symbols instead of strings as hash keys.
|
197
|
-
|
198
|
-
``` ruby
|
199
|
-
# bad
|
200
|
-
hash = { "one" => 1, "two" => 2, "three" => 3 }
|
201
|
-
|
202
|
-
# good
|
203
|
-
hash = { one: 1, two: 2, three: 3 }
|
204
|
-
```
|
205
|
-
|
206
|
-
## Documentation
|
207
|
-
|
208
|
-
Use [TomDoc](http://tomdoc.org) to the best of your ability. It's pretty sweet:
|
209
|
-
|
210
|
-
``` ruby
|
211
|
-
# Public: Duplicate some text an arbitrary number of times.
|
212
|
-
#
|
213
|
-
# text - The String to be duplicated.
|
214
|
-
# count - The Integer number of times to duplicate the text.
|
215
|
-
#
|
216
|
-
# Examples
|
217
|
-
#
|
218
|
-
# multiplex("Tom", 4)
|
219
|
-
# # => "TomTomTomTom"
|
220
|
-
#
|
221
|
-
# Returns the duplicated String.
|
222
|
-
def multiplex(text, count)
|
223
|
-
text * count
|
224
|
-
end
|
225
|
-
```
|
226
|
-
|
227
|
-
## Dynamic Dispatch
|
228
|
-
|
229
|
-
Avoid calling `send` and its cousins unless you really need it. Metaprogramming can be extremely powerful, but in most cases you can write code that captures your meaning by being explicit:
|
230
|
-
|
231
|
-
``` ruby
|
232
|
-
# avoid
|
233
|
-
unless [:base, :head].include?(base_or_head)
|
234
|
-
raise ArgumentError, "base_or_head must be either :base or :head"
|
235
|
-
end
|
236
|
-
|
237
|
-
repository = pull.send("#{base_or_head}_repository")
|
238
|
-
branch = pull.send("#{base_or_head}_ref_name")
|
239
|
-
|
240
|
-
# prefer
|
241
|
-
case base_or_head
|
242
|
-
when :base
|
243
|
-
repository = pull.base_repository
|
244
|
-
branch = pull.base_ref_name
|
245
|
-
when :head
|
246
|
-
repository = pull.head_repository
|
247
|
-
branch = pull.head_ref_name
|
248
|
-
else
|
249
|
-
raise ArgumentError, "base_or_head must be either :base or :head"
|
250
|
-
end
|
251
|
-
```
|
252
|
-
## Exceptions
|
253
|
-
|
254
|
-
* Don't use exceptions for flow of control.
|
255
|
-
|
256
|
-
``` ruby
|
257
|
-
# bad
|
258
|
-
begin
|
259
|
-
n / d
|
260
|
-
rescue ZeroDivisionError
|
261
|
-
puts "Cannot divide by 0!"
|
262
|
-
end
|
263
|
-
|
264
|
-
# good
|
265
|
-
if d.zero?
|
266
|
-
puts "Cannot divide by 0!"
|
267
|
-
else
|
268
|
-
n / d
|
269
|
-
end
|
270
|
-
```
|
271
|
-
|
272
|
-
* Rescue specific exceptions, not `StandardError` or its superclasses.
|
273
|
-
|
274
|
-
``` ruby
|
275
|
-
# bad
|
276
|
-
begin
|
277
|
-
# an exception occurs here
|
278
|
-
rescue
|
279
|
-
# exception handling
|
280
|
-
end
|
281
|
-
|
282
|
-
# still bad
|
283
|
-
begin
|
284
|
-
# an exception occurs here
|
285
|
-
rescue Exception
|
286
|
-
# exception handling
|
287
|
-
end
|
288
|
-
```
|
289
|
-
|
290
|
-
## Hashes
|
291
|
-
|
292
|
-
Use the Ruby 1.9 syntax for hash literals when all the keys are symbols:
|
293
|
-
|
294
|
-
``` ruby
|
295
|
-
# good
|
296
|
-
user = {
|
297
|
-
login: "defunkt",
|
298
|
-
name: "Chris Wanstrath"
|
299
|
-
}
|
300
|
-
|
301
|
-
# bad
|
302
|
-
user = {
|
303
|
-
:login => "defunkt",
|
304
|
-
:name => "Chris Wanstrath"
|
305
|
-
}
|
306
|
-
|
307
|
-
```
|
308
|
-
|
309
|
-
Use the 1.9 syntax when calling a method with Hash options arguments or named arguments:
|
310
|
-
|
311
|
-
``` ruby
|
312
|
-
# good
|
313
|
-
user = User.create(login: "jane")
|
314
|
-
link_to("Account", controller: "users", action: "show", id: user)
|
315
|
-
|
316
|
-
# bad
|
317
|
-
user = User.create(:login => "jane")
|
318
|
-
link_to("Account", :controller => "users", :action => "show", :id => user)
|
319
|
-
```
|
320
|
-
|
321
|
-
If you have a hash with mixed key types, use the legacy hashrocket style to avoid mixing styles within the same hash:
|
322
|
-
|
323
|
-
``` ruby
|
324
|
-
# good
|
325
|
-
hsh = {
|
326
|
-
:user_id => 55,
|
327
|
-
"followers-count" => 1000
|
328
|
-
}
|
329
|
-
|
330
|
-
# bad
|
331
|
-
hsh = {
|
332
|
-
user_id: 55,
|
333
|
-
"followers-count" => 1000
|
334
|
-
}
|
335
|
-
```
|
336
|
-
|
337
|
-
## Keyword Arguments
|
338
|
-
|
339
|
-
[Keyword arguments](http://magazine.rubyist.net/?Ruby200SpecialEn-kwarg) are recommended but not required when a method's arguments may otherwise be opaque or non-obvious when called. Additionally, prefer them over the old "Hash as pseudo-named args" style from pre-2.0 ruby.
|
340
|
-
|
341
|
-
So instead of this:
|
342
|
-
``` ruby
|
343
|
-
def remove_member(user, skip_membership_check=false)
|
344
|
-
# ...
|
345
|
-
end
|
346
|
-
|
347
|
-
# Elsewhere: what does true mean here?
|
348
|
-
remove_member(user, true)
|
349
|
-
```
|
350
|
-
|
351
|
-
Do this, which is much clearer.
|
352
|
-
``` ruby
|
353
|
-
def remove_member(user, skip_membership_check: false)
|
354
|
-
# ...
|
355
|
-
end
|
356
|
-
|
357
|
-
# Elsewhere, now with more clarity:
|
358
|
-
remove_member user, skip_membership_check: true
|
359
|
-
```
|
360
|
-
|
361
|
-
## Naming
|
362
|
-
|
363
|
-
* Use `snake_case` for methods and variables.
|
364
|
-
|
365
|
-
* Use `CamelCase` for classes and modules. (Keep acronyms like HTTP,
|
366
|
-
RFC, XML uppercase.)
|
367
|
-
|
368
|
-
* Use `SCREAMING_SNAKE_CASE` for other constants.
|
369
|
-
|
370
|
-
* The names of predicate methods (methods that return a boolean value)
|
371
|
-
should end in a question mark. (i.e. `Array#empty?`).
|
372
|
-
|
373
|
-
* The names of potentially "dangerous" methods (i.e. methods that modify `self` or the
|
374
|
-
arguments, `exit!`, etc.) should end with an exclamation mark. Bang methods
|
375
|
-
should only exist if a non-bang method exists. ([More on this](http://dablog.rubypal.com/2007/8/15/bang-methods-or-danger-will-rubyist)).
|
376
|
-
|
377
|
-
## Percent Literals
|
378
|
-
|
379
|
-
* Use `%w` freely.
|
380
|
-
|
381
|
-
``` ruby
|
382
|
-
STATES = %w(draft open closed)
|
383
|
-
```
|
384
|
-
|
385
|
-
* Use `%()` for single-line strings which require both interpolation
|
386
|
-
and embedded double-quotes. For multi-line strings, prefer heredocs.
|
387
|
-
|
388
|
-
``` ruby
|
389
|
-
# bad (no interpolation needed)
|
390
|
-
%(<div class="text">Some text</div>)
|
391
|
-
# should be "<div class=\"text\">Some text</div>"
|
392
|
-
|
393
|
-
# bad (no double-quotes)
|
394
|
-
%(This is #{quality} style)
|
395
|
-
# should be "This is #{quality} style"
|
396
|
-
|
397
|
-
# bad (multiple lines)
|
398
|
-
%(<div>\n<span class="big">#{exclamation}</span>\n</div>)
|
399
|
-
# should be a heredoc.
|
400
|
-
|
401
|
-
# good (requires interpolation, has quotes, single line)
|
402
|
-
%(<tr><td class="name">#{name}</td>)
|
403
|
-
```
|
404
|
-
|
405
|
-
* Use `%r` only for regular expressions matching *more than* one '/' character.
|
406
|
-
|
407
|
-
``` ruby
|
408
|
-
# bad
|
409
|
-
%r(\s+)
|
410
|
-
|
411
|
-
# still bad
|
412
|
-
%r(^/(.*)$)
|
413
|
-
# should be /^\/(.*)$/
|
414
|
-
|
415
|
-
# good
|
416
|
-
%r(^/blog/2011/(.*)$)
|
417
|
-
```
|
418
|
-
|
419
|
-
## Regular Expressions
|
420
|
-
|
421
|
-
* Avoid using $1-9 as it can be hard to track what they contain. Named groups
|
422
|
-
can be used instead.
|
423
|
-
|
424
|
-
``` ruby
|
425
|
-
# bad
|
426
|
-
/(regexp)/ =~ string
|
427
|
-
...
|
428
|
-
process $1
|
429
|
-
|
430
|
-
# good
|
431
|
-
/(?<meaningful_var>regexp)/ =~ string
|
432
|
-
...
|
433
|
-
process meaningful_var
|
434
|
-
```
|
435
|
-
|
436
|
-
* Be careful with `^` and `$` as they match start/end of line, not string endings.
|
437
|
-
If you want to match the whole string use: `\A` and `\z`.
|
438
|
-
|
439
|
-
``` ruby
|
440
|
-
string = "some injection\nusername"
|
441
|
-
string[/^username$/] # matches
|
442
|
-
string[/\Ausername\z/] # don't match
|
443
|
-
```
|
444
|
-
|
445
|
-
* Use `x` modifier for complex regexps. This makes them more readable and you
|
446
|
-
can add some useful comments. Just be careful as spaces are ignored.
|
447
|
-
|
448
|
-
``` ruby
|
449
|
-
regexp = %r{
|
450
|
-
start # some text
|
451
|
-
\s # white space char
|
452
|
-
(group) # first group
|
453
|
-
(?:alt1|alt2) # some alternation
|
454
|
-
end
|
455
|
-
}x
|
456
|
-
```
|
457
|
-
|
458
|
-
## Requires
|
459
|
-
|
460
|
-
Always `require` dependencies used directly in a script at the start of the same file.
|
461
|
-
Resources that will get autoloaded on first use—such as Rails models, controllers, or
|
462
|
-
helpers—don't need to be required.
|
463
|
-
|
464
|
-
``` ruby
|
465
|
-
require "set"
|
466
|
-
require "time"
|
467
|
-
|
468
|
-
%w(foo bar).to_set
|
469
|
-
Time.parse("2015-10-21")
|
470
|
-
```
|
471
|
-
|
472
|
-
This not only loads the necessary dependencies if they haven't already, but acts as
|
473
|
-
documentation about the libraries that the current file uses.
|
474
|
-
|
475
|
-
## Strings
|
476
|
-
|
477
|
-
* Prefer string interpolation instead of string concatenation:
|
478
|
-
|
479
|
-
``` ruby
|
480
|
-
# bad
|
481
|
-
email_with_name = user.name + " <" + user.email + ">"
|
482
|
-
|
483
|
-
# good
|
484
|
-
email_with_name = "#{user.name} <#{user.email}>"
|
485
|
-
```
|
486
|
-
|
487
|
-
* Use double-quoted strings. Interpolation and escaped characters
|
488
|
-
will always work without a delimiter change, and `'` is a lot more
|
489
|
-
common than `"` in string literals.
|
490
|
-
|
491
|
-
``` ruby
|
492
|
-
# bad
|
493
|
-
name = 'Bozhidar'
|
494
|
-
|
495
|
-
# good
|
496
|
-
name = "Bozhidar"
|
497
|
-
```
|
498
|
-
|
499
|
-
* Avoid using `String#+` when you need to construct large data chunks.
|
500
|
-
Instead, use `String#<<`. Concatenation mutates the string instance in-place
|
501
|
-
and is always faster than `String#+`, which creates a bunch of new string objects.
|
502
|
-
|
503
|
-
``` ruby
|
504
|
-
# good and also fast
|
505
|
-
html = ""
|
506
|
-
html << "<h1>Page title</h1>"
|
507
|
-
|
508
|
-
paragraphs.each do |paragraph|
|
509
|
-
html << "<p>#{paragraph}</p>"
|
510
|
-
end
|
511
|
-
```
|
512
|
-
|
513
|
-
## Syntax
|
514
|
-
|
515
|
-
* Use `def` with parentheses when there are arguments. Omit the
|
516
|
-
parentheses when the method doesn't accept any arguments.
|
517
|
-
|
518
|
-
``` ruby
|
519
|
-
def some_method
|
520
|
-
# body omitted
|
521
|
-
end
|
522
|
-
|
523
|
-
def some_method_with_arguments(arg1, arg2)
|
524
|
-
# body omitted
|
525
|
-
end
|
526
|
-
```
|
527
|
-
|
528
|
-
* Never use `for`, unless you know exactly why. Most of the time iterators
|
529
|
-
should be used instead. `for` is implemented in terms of `each` (so
|
530
|
-
you're adding a level of indirection), but with a twist - `for`
|
531
|
-
doesn't introduce a new scope (unlike `each`) and variables defined
|
532
|
-
in its block will be visible outside it.
|
533
|
-
|
534
|
-
``` ruby
|
535
|
-
arr = [1, 2, 3]
|
536
|
-
|
537
|
-
# bad
|
538
|
-
for elem in arr do
|
539
|
-
puts elem
|
540
|
-
end
|
541
|
-
|
542
|
-
# good
|
543
|
-
arr.each { |elem| puts elem }
|
544
|
-
```
|
545
|
-
|
546
|
-
* Never use `then` for multi-line `if/unless`.
|
547
|
-
|
548
|
-
``` ruby
|
549
|
-
# bad
|
550
|
-
if some_condition then
|
551
|
-
# body omitted
|
552
|
-
end
|
553
|
-
|
554
|
-
# good
|
555
|
-
if some_condition
|
556
|
-
# body omitted
|
557
|
-
end
|
558
|
-
```
|
559
|
-
|
560
|
-
* Avoid the ternary operator (`?:`) except in cases where all expressions are extremely
|
561
|
-
trivial. However, do use the ternary operator(`?:`) over `if/then/else/end` constructs
|
562
|
-
for single line conditionals.
|
563
|
-
|
564
|
-
``` ruby
|
565
|
-
# bad
|
566
|
-
result = if some_condition then something else something_else end
|
567
|
-
|
568
|
-
# good
|
569
|
-
result = some_condition ? something : something_else
|
570
|
-
```
|
571
|
-
|
572
|
-
* Use one expression per branch in a ternary operator. This
|
573
|
-
also means that ternary operators must not be nested. Prefer
|
574
|
-
`if/else` constructs in these cases.
|
575
|
-
|
576
|
-
``` ruby
|
577
|
-
# bad
|
578
|
-
some_condition ? (nested_condition ? nested_something : nested_something_else) : something_else
|
579
|
-
|
580
|
-
# good
|
581
|
-
if some_condition
|
582
|
-
nested_condition ? nested_something : nested_something_else
|
583
|
-
else
|
584
|
-
something_else
|
585
|
-
end
|
586
|
-
```
|
587
|
-
|
588
|
-
* The `and` and `or` keywords are banned. It's just not worth it. Always use `&&` and `||` instead.
|
589
|
-
|
590
|
-
* Avoid multi-line `?:` (the ternary operator), use `if/unless` instead.
|
591
|
-
|
592
|
-
* Favor modifier `if/unless` usage when you have a single-line
|
593
|
-
body.
|
594
|
-
|
595
|
-
``` ruby
|
596
|
-
# bad
|
597
|
-
if some_condition
|
598
|
-
do_something
|
599
|
-
end
|
600
|
-
|
601
|
-
# good
|
602
|
-
do_something if some_condition
|
603
|
-
```
|
604
|
-
|
605
|
-
* Never use `unless` with `else`. Rewrite these with the positive case first.
|
606
|
-
|
607
|
-
``` ruby
|
608
|
-
# bad
|
609
|
-
unless success?
|
610
|
-
puts "failure"
|
611
|
-
else
|
612
|
-
puts "success"
|
613
|
-
end
|
614
|
-
|
615
|
-
# good
|
616
|
-
if success?
|
617
|
-
puts "success"
|
618
|
-
else
|
619
|
-
puts "failure"
|
620
|
-
end
|
621
|
-
```
|
622
|
-
|
623
|
-
* Don't use parentheses around the condition of an `if/unless/while`.
|
624
|
-
|
625
|
-
``` ruby
|
626
|
-
# bad
|
627
|
-
if (x > 10)
|
628
|
-
# body omitted
|
629
|
-
end
|
630
|
-
|
631
|
-
# good
|
632
|
-
if x > 10
|
633
|
-
# body omitted
|
634
|
-
end
|
635
|
-
```
|
636
|
-
|
637
|
-
* Prefer `{...}` over `do...end` for single-line blocks. Avoid using
|
638
|
-
`{...}` for multi-line blocks (multiline chaining is always
|
639
|
-
ugly). Always use `do...end` for "control flow" and "method
|
640
|
-
definitions" (e.g. in Rakefiles and certain DSLs). Avoid `do...end`
|
641
|
-
when chaining.
|
642
|
-
|
643
|
-
``` ruby
|
644
|
-
names = ["Bozhidar", "Steve", "Sarah"]
|
645
|
-
|
646
|
-
# good
|
647
|
-
names.each { |name| puts name }
|
648
|
-
|
649
|
-
# bad
|
650
|
-
names.each do |name|
|
651
|
-
puts name
|
652
|
-
end
|
653
|
-
|
654
|
-
# good
|
655
|
-
names.select { |name| name.start_with?("S") }.map { |name| name.upcase }
|
656
|
-
|
657
|
-
# bad
|
658
|
-
names.select do |name|
|
659
|
-
name.start_with?("S")
|
660
|
-
end.map { |name| name.upcase }
|
661
|
-
```
|
662
|
-
|
663
|
-
Some will argue that multiline chaining would look OK with the use of {...}, but they should
|
664
|
-
ask themselves - is this code really readable and can't the block's contents be extracted into
|
665
|
-
nifty methods?
|
666
|
-
|
667
|
-
* Avoid `return` where not required.
|
668
|
-
|
669
|
-
``` ruby
|
670
|
-
# bad
|
671
|
-
def some_method(some_arr)
|
672
|
-
return some_arr.size
|
673
|
-
end
|
674
|
-
|
675
|
-
# good
|
676
|
-
def some_method(some_arr)
|
677
|
-
some_arr.size
|
678
|
-
end
|
679
|
-
```
|
680
|
-
|
681
|
-
* Use spaces around the `=` operator when assigning default values to method parameters:
|
682
|
-
|
683
|
-
``` ruby
|
684
|
-
# bad
|
685
|
-
def some_method(arg1=:default, arg2=nil, arg3=[])
|
686
|
-
# do something...
|
687
|
-
end
|
688
|
-
|
689
|
-
# good
|
690
|
-
def some_method(arg1 = :default, arg2 = nil, arg3 = [])
|
691
|
-
# do something...
|
692
|
-
end
|
693
|
-
```
|
694
|
-
|
695
|
-
While several Ruby books suggest the first style, the second is much more prominent
|
696
|
-
in practice (and arguably a bit more readable).
|
697
|
-
|
698
|
-
* Using the return value of `=` (an assignment) is ok.
|
699
|
-
|
700
|
-
``` ruby
|
701
|
-
# bad
|
702
|
-
if (v = array.grep(/foo/)) ...
|
703
|
-
|
704
|
-
# good
|
705
|
-
if v = array.grep(/foo/) ...
|
706
|
-
|
707
|
-
# also good - has correct precedence.
|
708
|
-
if (v = next_value) == "hello" ...
|
709
|
-
```
|
710
|
-
|
711
|
-
* Use `||=` freely to initialize variables.
|
712
|
-
|
713
|
-
``` ruby
|
714
|
-
# set name to Bozhidar, only if it's nil or false
|
715
|
-
name ||= "Bozhidar"
|
716
|
-
```
|
717
|
-
|
718
|
-
* Don't use `||=` to initialize boolean variables. (Consider what
|
719
|
-
would happen if the current value happened to be `false`.)
|
720
|
-
|
721
|
-
``` ruby
|
722
|
-
# bad - would set enabled to true even if it was false
|
723
|
-
enabled ||= true
|
724
|
-
|
725
|
-
# good
|
726
|
-
enabled = true if enabled.nil?
|
727
|
-
```
|
728
|
-
|
729
|
-
* Avoid using Perl-style special variables (like `$0-9`, `$`,
|
730
|
-
etc. ). They are quite cryptic and their use in anything but
|
731
|
-
one-liner scripts is discouraged. Prefer long form versions such as
|
732
|
-
`$PROGRAM_NAME`.
|
733
|
-
|
734
|
-
* Never put a space between a method name and the opening parenthesis.
|
735
|
-
|
736
|
-
``` ruby
|
737
|
-
# bad
|
738
|
-
f (3 + 2) + 1
|
739
|
-
|
740
|
-
# good
|
741
|
-
f(3 + 2) + 1
|
742
|
-
```
|
743
|
-
|
744
|
-
* If the first argument to a method begins with an open parenthesis,
|
745
|
-
always use parentheses in the method invocation. For example, write
|
746
|
-
`f((3 + 2) + 1)`.
|
747
|
-
|
748
|
-
* Use `_` for unused block parameters.
|
749
|
-
|
750
|
-
``` ruby
|
751
|
-
# bad
|
752
|
-
result = hash.map { |k, v| v + 1 }
|
753
|
-
|
754
|
-
# good
|
755
|
-
result = hash.map { |_, v| v + 1 }
|
756
|
-
```
|
757
|
-
|
758
|
-
* Don't use the `===` (threequals) operator to check types. `===` is mostly an
|
759
|
-
implementation detail to support Ruby features like `case`, and it's not commutative.
|
760
|
-
For example, `String === "hi"` is true and `"hi" === String` is false.
|
761
|
-
Instead, use `is_a?` or `kind_of?` if you must.
|
762
|
-
|
763
|
-
Refactoring is even better. It's worth looking hard at any code that explicitly checks types.
|