rubocop-github 0.2.3 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/STYLEGUIDE.md +738 -0
- data/config/default.yml +41 -4
- data/config/rails.yml +4 -0
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 601fcd9953677e8b83264571ab930fb673ce9308
|
4
|
+
data.tar.gz: 7cb82de90efa33ab5bd9f2123938f49a131cb448
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0ac31931fe1edf63b4ecb4bf2f1177ea2b7a68a7db69e3ffdb802aaac8be2c4f122c88ac755a9d197d8fa4676b1d3ec2a4717e615b66e1afb07d385f96ed99e8
|
7
|
+
data.tar.gz: a7da9a85c62522bfdee8e3f9a97b4c303ae49af3baf97ab1a647e9b4e3d44042f53a3c646f3b47f5689ce844fee212bda37bc1fae27e574da7eca52834f6af51
|
data/STYLEGUIDE.md
ADDED
@@ -0,0 +1,738 @@
|
|
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
|
+
## Exceptions
|
228
|
+
|
229
|
+
* Don't use exceptions for flow of control.
|
230
|
+
|
231
|
+
``` ruby
|
232
|
+
# bad
|
233
|
+
begin
|
234
|
+
n / d
|
235
|
+
rescue ZeroDivisionError
|
236
|
+
puts "Cannot divide by 0!"
|
237
|
+
end
|
238
|
+
|
239
|
+
# good
|
240
|
+
if d.zero?
|
241
|
+
puts "Cannot divide by 0!"
|
242
|
+
else
|
243
|
+
n / d
|
244
|
+
end
|
245
|
+
```
|
246
|
+
|
247
|
+
* Rescue specific exceptions, not `StandardError` or its superclasses.
|
248
|
+
|
249
|
+
``` ruby
|
250
|
+
# bad
|
251
|
+
begin
|
252
|
+
# an exception occurs here
|
253
|
+
rescue
|
254
|
+
# exception handling
|
255
|
+
end
|
256
|
+
|
257
|
+
# still bad
|
258
|
+
begin
|
259
|
+
# an exception occurs here
|
260
|
+
rescue Exception
|
261
|
+
# exception handling
|
262
|
+
end
|
263
|
+
```
|
264
|
+
|
265
|
+
## Hashes
|
266
|
+
|
267
|
+
Use the Ruby 1.9 syntax for hash literals when all the keys are symbols:
|
268
|
+
|
269
|
+
``` ruby
|
270
|
+
# good
|
271
|
+
user = {
|
272
|
+
login: "defunkt",
|
273
|
+
name: "Chris Wanstrath"
|
274
|
+
}
|
275
|
+
|
276
|
+
# bad
|
277
|
+
user = {
|
278
|
+
:login => "defunkt",
|
279
|
+
:name => "Chris Wanstrath"
|
280
|
+
}
|
281
|
+
|
282
|
+
```
|
283
|
+
|
284
|
+
Use the 1.9 syntax when calling a method with Hash options arguments or named arguments:
|
285
|
+
|
286
|
+
``` ruby
|
287
|
+
# good
|
288
|
+
user = User.create(login: "jane")
|
289
|
+
link_to("Account", controller: "users", action: "show", id: user)
|
290
|
+
|
291
|
+
# bad
|
292
|
+
user = User.create(:login => "jane")
|
293
|
+
link_to("Account", :controller => "users", :action => "show", :id => user)
|
294
|
+
```
|
295
|
+
|
296
|
+
If you have a hash with mixed key types, use the legacy hashrocket style to avoid mixing styles within the same hash:
|
297
|
+
|
298
|
+
``` ruby
|
299
|
+
# good
|
300
|
+
hsh = {
|
301
|
+
:user_id => 55,
|
302
|
+
"followers-count" => 1000
|
303
|
+
}
|
304
|
+
|
305
|
+
# bad
|
306
|
+
hsh = {
|
307
|
+
user_id: 55,
|
308
|
+
"followers-count" => 1000
|
309
|
+
}
|
310
|
+
```
|
311
|
+
|
312
|
+
## Keyword Arguments
|
313
|
+
|
314
|
+
[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.
|
315
|
+
|
316
|
+
So instead of this:
|
317
|
+
``` ruby
|
318
|
+
def remove_member(user, skip_membership_check=false)
|
319
|
+
# ...
|
320
|
+
end
|
321
|
+
|
322
|
+
# Elsewhere: what does true mean here?
|
323
|
+
remove_member(user, true)
|
324
|
+
```
|
325
|
+
|
326
|
+
Do this, which is much clearer.
|
327
|
+
``` ruby
|
328
|
+
def remove_member(user, skip_membership_check: false)
|
329
|
+
# ...
|
330
|
+
end
|
331
|
+
|
332
|
+
# Elsewhere, now with more clarity:
|
333
|
+
remove_member user, skip_membership_check: true
|
334
|
+
```
|
335
|
+
|
336
|
+
## Naming
|
337
|
+
|
338
|
+
* Use `snake_case` for methods and variables.
|
339
|
+
|
340
|
+
* Use `CamelCase` for classes and modules. (Keep acronyms like HTTP,
|
341
|
+
RFC, XML uppercase.)
|
342
|
+
|
343
|
+
* Use `SCREAMING_SNAKE_CASE` for other constants.
|
344
|
+
|
345
|
+
* The names of predicate methods (methods that return a boolean value)
|
346
|
+
should end in a question mark. (i.e. `Array#empty?`).
|
347
|
+
|
348
|
+
* The names of potentially "dangerous" methods (i.e. methods that modify `self` or the
|
349
|
+
arguments, `exit!`, etc.) should end with an exclamation mark. Bang methods
|
350
|
+
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)).
|
351
|
+
|
352
|
+
## Percent Literals
|
353
|
+
|
354
|
+
* Use `%w` freely.
|
355
|
+
|
356
|
+
``` ruby
|
357
|
+
STATES = %w(draft open closed)
|
358
|
+
```
|
359
|
+
|
360
|
+
* Use `%()` for single-line strings which require both interpolation
|
361
|
+
and embedded double-quotes. For multi-line strings, prefer heredocs.
|
362
|
+
|
363
|
+
``` ruby
|
364
|
+
# bad (no interpolation needed)
|
365
|
+
%(<div class="text">Some text</div>)
|
366
|
+
# should be "<div class=\"text\">Some text</div>"
|
367
|
+
|
368
|
+
# bad (no double-quotes)
|
369
|
+
%(This is #{quality} style)
|
370
|
+
# should be "This is #{quality} style"
|
371
|
+
|
372
|
+
# bad (multiple lines)
|
373
|
+
%(<div>\n<span class="big">#{exclamation}</span>\n</div>)
|
374
|
+
# should be a heredoc.
|
375
|
+
|
376
|
+
# good (requires interpolation, has quotes, single line)
|
377
|
+
%(<tr><td class="name">#{name}</td>)
|
378
|
+
```
|
379
|
+
|
380
|
+
* Use `%r` only for regular expressions matching *more than* one '/' character.
|
381
|
+
|
382
|
+
``` ruby
|
383
|
+
# bad
|
384
|
+
%r(\s+)
|
385
|
+
|
386
|
+
# still bad
|
387
|
+
%r(^/(.*)$)
|
388
|
+
# should be /^\/(.*)$/
|
389
|
+
|
390
|
+
# good
|
391
|
+
%r(^/blog/2011/(.*)$)
|
392
|
+
```
|
393
|
+
|
394
|
+
## Regular Expressions
|
395
|
+
|
396
|
+
* Avoid using $1-9 as it can be hard to track what they contain. Named groups
|
397
|
+
can be used instead.
|
398
|
+
|
399
|
+
``` ruby
|
400
|
+
# bad
|
401
|
+
/(regexp)/ =~ string
|
402
|
+
...
|
403
|
+
process $1
|
404
|
+
|
405
|
+
# good
|
406
|
+
/(?<meaningful_var>regexp)/ =~ string
|
407
|
+
...
|
408
|
+
process meaningful_var
|
409
|
+
```
|
410
|
+
|
411
|
+
* Be careful with `^` and `$` as they match start/end of line, not string endings.
|
412
|
+
If you want to match the whole string use: `\A` and `\z`.
|
413
|
+
|
414
|
+
``` ruby
|
415
|
+
string = "some injection\nusername"
|
416
|
+
string[/^username$/] # matches
|
417
|
+
string[/\Ausername\z/] # don't match
|
418
|
+
```
|
419
|
+
|
420
|
+
* Use `x` modifier for complex regexps. This makes them more readable and you
|
421
|
+
can add some useful comments. Just be careful as spaces are ignored.
|
422
|
+
|
423
|
+
``` ruby
|
424
|
+
regexp = %r{
|
425
|
+
start # some text
|
426
|
+
\s # white space char
|
427
|
+
(group) # first group
|
428
|
+
(?:alt1|alt2) # some alternation
|
429
|
+
end
|
430
|
+
}x
|
431
|
+
```
|
432
|
+
|
433
|
+
## Requires
|
434
|
+
|
435
|
+
Always `require` dependencies used directly in a script at the start of the same file.
|
436
|
+
Resources that will get autoloaded on first use—such as Rails models, controllers, or
|
437
|
+
helpers—don't need to be required.
|
438
|
+
|
439
|
+
``` ruby
|
440
|
+
require "set"
|
441
|
+
require "time"
|
442
|
+
|
443
|
+
%w(foo bar).to_set
|
444
|
+
Time.parse("2015-10-21")
|
445
|
+
```
|
446
|
+
|
447
|
+
This not only loads the necessary dependencies if they haven't already, but acts as
|
448
|
+
documentation about the libraries that the current file uses.
|
449
|
+
|
450
|
+
## Strings
|
451
|
+
|
452
|
+
* Prefer string interpolation instead of string concatenation:
|
453
|
+
|
454
|
+
``` ruby
|
455
|
+
# bad
|
456
|
+
email_with_name = user.name + " <" + user.email + ">"
|
457
|
+
|
458
|
+
# good
|
459
|
+
email_with_name = "#{user.name} <#{user.email}>"
|
460
|
+
```
|
461
|
+
|
462
|
+
* Use double-quoted strings. Interpolation and escaped characters
|
463
|
+
will always work without a delimiter change, and `'` is a lot more
|
464
|
+
common than `"` in string literals.
|
465
|
+
|
466
|
+
``` ruby
|
467
|
+
# bad
|
468
|
+
name = 'Bozhidar'
|
469
|
+
|
470
|
+
# good
|
471
|
+
name = "Bozhidar"
|
472
|
+
```
|
473
|
+
|
474
|
+
* Avoid using `String#+` when you need to construct large data chunks.
|
475
|
+
Instead, use `String#<<`. Concatenation mutates the string instance in-place
|
476
|
+
and is always faster than `String#+`, which creates a bunch of new string objects.
|
477
|
+
|
478
|
+
``` ruby
|
479
|
+
# good and also fast
|
480
|
+
html = ""
|
481
|
+
html << "<h1>Page title</h1>"
|
482
|
+
|
483
|
+
paragraphs.each do |paragraph|
|
484
|
+
html << "<p>#{paragraph}</p>"
|
485
|
+
end
|
486
|
+
```
|
487
|
+
|
488
|
+
## Syntax
|
489
|
+
|
490
|
+
* Use `def` with parentheses when there are arguments. Omit the
|
491
|
+
parentheses when the method doesn't accept any arguments.
|
492
|
+
|
493
|
+
``` ruby
|
494
|
+
def some_method
|
495
|
+
# body omitted
|
496
|
+
end
|
497
|
+
|
498
|
+
def some_method_with_arguments(arg1, arg2)
|
499
|
+
# body omitted
|
500
|
+
end
|
501
|
+
```
|
502
|
+
|
503
|
+
* Never use `for`, unless you know exactly why. Most of the time iterators
|
504
|
+
should be used instead. `for` is implemented in terms of `each` (so
|
505
|
+
you're adding a level of indirection), but with a twist - `for`
|
506
|
+
doesn't introduce a new scope (unlike `each`) and variables defined
|
507
|
+
in its block will be visible outside it.
|
508
|
+
|
509
|
+
``` ruby
|
510
|
+
arr = [1, 2, 3]
|
511
|
+
|
512
|
+
# bad
|
513
|
+
for elem in arr do
|
514
|
+
puts elem
|
515
|
+
end
|
516
|
+
|
517
|
+
# good
|
518
|
+
arr.each { |elem| puts elem }
|
519
|
+
```
|
520
|
+
|
521
|
+
* Never use `then` for multi-line `if/unless`.
|
522
|
+
|
523
|
+
``` ruby
|
524
|
+
# bad
|
525
|
+
if some_condition then
|
526
|
+
# body omitted
|
527
|
+
end
|
528
|
+
|
529
|
+
# good
|
530
|
+
if some_condition
|
531
|
+
# body omitted
|
532
|
+
end
|
533
|
+
```
|
534
|
+
|
535
|
+
* Avoid the ternary operator (`?:`) except in cases where all expressions are extremely
|
536
|
+
trivial. However, do use the ternary operator(`?:`) over `if/then/else/end` constructs
|
537
|
+
for single line conditionals.
|
538
|
+
|
539
|
+
``` ruby
|
540
|
+
# bad
|
541
|
+
result = if some_condition then something else something_else end
|
542
|
+
|
543
|
+
# good
|
544
|
+
result = some_condition ? something : something_else
|
545
|
+
```
|
546
|
+
|
547
|
+
* Use one expression per branch in a ternary operator. This
|
548
|
+
also means that ternary operators must not be nested. Prefer
|
549
|
+
`if/else` constructs in these cases.
|
550
|
+
|
551
|
+
``` ruby
|
552
|
+
# bad
|
553
|
+
some_condition ? (nested_condition ? nested_something : nested_something_else) : something_else
|
554
|
+
|
555
|
+
# good
|
556
|
+
if some_condition
|
557
|
+
nested_condition ? nested_something : nested_something_else
|
558
|
+
else
|
559
|
+
something_else
|
560
|
+
end
|
561
|
+
```
|
562
|
+
|
563
|
+
* The `and` and `or` keywords are banned. It's just not worth it. Always use `&&` and `||` instead.
|
564
|
+
|
565
|
+
* Avoid multi-line `?:` (the ternary operator), use `if/unless` instead.
|
566
|
+
|
567
|
+
* Favor modifier `if/unless` usage when you have a single-line
|
568
|
+
body.
|
569
|
+
|
570
|
+
``` ruby
|
571
|
+
# bad
|
572
|
+
if some_condition
|
573
|
+
do_something
|
574
|
+
end
|
575
|
+
|
576
|
+
# good
|
577
|
+
do_something if some_condition
|
578
|
+
```
|
579
|
+
|
580
|
+
* Never use `unless` with `else`. Rewrite these with the positive case first.
|
581
|
+
|
582
|
+
``` ruby
|
583
|
+
# bad
|
584
|
+
unless success?
|
585
|
+
puts "failure"
|
586
|
+
else
|
587
|
+
puts "success"
|
588
|
+
end
|
589
|
+
|
590
|
+
# good
|
591
|
+
if success?
|
592
|
+
puts "success"
|
593
|
+
else
|
594
|
+
puts "failure"
|
595
|
+
end
|
596
|
+
```
|
597
|
+
|
598
|
+
* Don't use parentheses around the condition of an `if/unless/while`.
|
599
|
+
|
600
|
+
``` ruby
|
601
|
+
# bad
|
602
|
+
if (x > 10)
|
603
|
+
# body omitted
|
604
|
+
end
|
605
|
+
|
606
|
+
# good
|
607
|
+
if x > 10
|
608
|
+
# body omitted
|
609
|
+
end
|
610
|
+
```
|
611
|
+
|
612
|
+
* Prefer `{...}` over `do...end` for single-line blocks. Avoid using
|
613
|
+
`{...}` for multi-line blocks (multiline chaining is always
|
614
|
+
ugly). Always use `do...end` for "control flow" and "method
|
615
|
+
definitions" (e.g. in Rakefiles and certain DSLs). Avoid `do...end`
|
616
|
+
when chaining.
|
617
|
+
|
618
|
+
``` ruby
|
619
|
+
names = ["Bozhidar", "Steve", "Sarah"]
|
620
|
+
|
621
|
+
# good
|
622
|
+
names.each { |name| puts name }
|
623
|
+
|
624
|
+
# bad
|
625
|
+
names.each do |name|
|
626
|
+
puts name
|
627
|
+
end
|
628
|
+
|
629
|
+
# good
|
630
|
+
names.select { |name| name.start_with?("S") }.map { |name| name.upcase }
|
631
|
+
|
632
|
+
# bad
|
633
|
+
names.select do |name|
|
634
|
+
name.start_with?("S")
|
635
|
+
end.map { |name| name.upcase }
|
636
|
+
```
|
637
|
+
|
638
|
+
Some will argue that multiline chaining would look OK with the use of {...}, but they should
|
639
|
+
ask themselves - is this code really readable and can't the block's contents be extracted into
|
640
|
+
nifty methods?
|
641
|
+
|
642
|
+
* Avoid `return` where not required.
|
643
|
+
|
644
|
+
``` ruby
|
645
|
+
# bad
|
646
|
+
def some_method(some_arr)
|
647
|
+
return some_arr.size
|
648
|
+
end
|
649
|
+
|
650
|
+
# good
|
651
|
+
def some_method(some_arr)
|
652
|
+
some_arr.size
|
653
|
+
end
|
654
|
+
```
|
655
|
+
|
656
|
+
* Use spaces around the `=` operator when assigning default values to method parameters:
|
657
|
+
|
658
|
+
``` ruby
|
659
|
+
# bad
|
660
|
+
def some_method(arg1=:default, arg2=nil, arg3=[])
|
661
|
+
# do something...
|
662
|
+
end
|
663
|
+
|
664
|
+
# good
|
665
|
+
def some_method(arg1 = :default, arg2 = nil, arg3 = [])
|
666
|
+
# do something...
|
667
|
+
end
|
668
|
+
```
|
669
|
+
|
670
|
+
While several Ruby books suggest the first style, the second is much more prominent
|
671
|
+
in practice (and arguably a bit more readable).
|
672
|
+
|
673
|
+
* Using the return value of `=` (an assignment) is ok.
|
674
|
+
|
675
|
+
``` ruby
|
676
|
+
# bad
|
677
|
+
if (v = array.grep(/foo/)) ...
|
678
|
+
|
679
|
+
# good
|
680
|
+
if v = array.grep(/foo/) ...
|
681
|
+
|
682
|
+
# also good - has correct precedence.
|
683
|
+
if (v = next_value) == "hello" ...
|
684
|
+
```
|
685
|
+
|
686
|
+
* Use `||=` freely to initialize variables.
|
687
|
+
|
688
|
+
``` ruby
|
689
|
+
# set name to Bozhidar, only if it's nil or false
|
690
|
+
name ||= "Bozhidar"
|
691
|
+
```
|
692
|
+
|
693
|
+
* Don't use `||=` to initialize boolean variables. (Consider what
|
694
|
+
would happen if the current value happened to be `false`.)
|
695
|
+
|
696
|
+
``` ruby
|
697
|
+
# bad - would set enabled to true even if it was false
|
698
|
+
enabled ||= true
|
699
|
+
|
700
|
+
# good
|
701
|
+
enabled = true if enabled.nil?
|
702
|
+
```
|
703
|
+
|
704
|
+
* Avoid using Perl-style special variables (like `$0-9`, `$`,
|
705
|
+
etc. ). They are quite cryptic and their use in anything but
|
706
|
+
one-liner scripts is discouraged. Prefer long form versions such as
|
707
|
+
`$PROGRAM_NAME`.
|
708
|
+
|
709
|
+
* Never put a space between a method name and the opening parenthesis.
|
710
|
+
|
711
|
+
``` ruby
|
712
|
+
# bad
|
713
|
+
f (3 + 2) + 1
|
714
|
+
|
715
|
+
# good
|
716
|
+
f(3 + 2) + 1
|
717
|
+
```
|
718
|
+
|
719
|
+
* If the first argument to a method begins with an open parenthesis,
|
720
|
+
always use parentheses in the method invocation. For example, write
|
721
|
+
`f((3 + 2) + 1)`.
|
722
|
+
|
723
|
+
* Use `_` for unused block parameters.
|
724
|
+
|
725
|
+
``` ruby
|
726
|
+
# bad
|
727
|
+
result = hash.map { |k, v| v + 1 }
|
728
|
+
|
729
|
+
# good
|
730
|
+
result = hash.map { |_, v| v + 1 }
|
731
|
+
```
|
732
|
+
|
733
|
+
* Don't use the `===` (threequals) operator to check types. `===` is mostly an
|
734
|
+
implementation detail to support Ruby features like `case`, and it's not commutative.
|
735
|
+
For example, `String === "hi"` is true and `"hi" === String` is false.
|
736
|
+
Instead, use `is_a?` or `kind_of?` if you must.
|
737
|
+
|
738
|
+
Refactoring is even better. It's worth looking hard at any code that explicitly checks types.
|
data/config/default.yml
CHANGED
@@ -48,15 +48,16 @@ Lint/EndAlignment:
|
|
48
48
|
Lint/EnsureReturn:
|
49
49
|
Enabled: true
|
50
50
|
|
51
|
-
Lint/Eval:
|
52
|
-
Enabled: true
|
53
|
-
|
54
51
|
Lint/FloatOutOfRange:
|
55
52
|
Enabled: true
|
56
53
|
|
57
54
|
Lint/FormatParameterMismatch:
|
58
55
|
Enabled: true
|
59
56
|
|
57
|
+
Style/HashSyntax:
|
58
|
+
Enabled: true
|
59
|
+
EnforcedStyle: ruby19
|
60
|
+
|
60
61
|
Lint/InvalidCharacterLiteral:
|
61
62
|
Enabled: true
|
62
63
|
|
@@ -105,6 +106,36 @@ Lint/UselessSetterCall:
|
|
105
106
|
Lint/Void:
|
106
107
|
Enabled: true
|
107
108
|
|
109
|
+
Metrics/AbcSize:
|
110
|
+
Enabled: false
|
111
|
+
|
112
|
+
Metrics/BlockLength:
|
113
|
+
Enabled: false
|
114
|
+
|
115
|
+
Metrics/BlockNesting:
|
116
|
+
Enabled: false
|
117
|
+
|
118
|
+
Metrics/ClassLength:
|
119
|
+
Enabled: false
|
120
|
+
|
121
|
+
Metrics/CyclomaticComplexity:
|
122
|
+
Enabled: false
|
123
|
+
|
124
|
+
Metrics/LineLength:
|
125
|
+
Enabled: false
|
126
|
+
|
127
|
+
Metrics/MethodLength:
|
128
|
+
Enabled: false
|
129
|
+
|
130
|
+
Metrics/ModuleLength:
|
131
|
+
Enabled: false
|
132
|
+
|
133
|
+
Metrics/ParameterLists:
|
134
|
+
Enabled: false
|
135
|
+
|
136
|
+
Metrics/PerceivedComplexity:
|
137
|
+
Enabled: false
|
138
|
+
|
108
139
|
Performance/CaseWhenSplat:
|
109
140
|
Enabled: false
|
110
141
|
|
@@ -154,6 +185,9 @@ Performance/Size:
|
|
154
185
|
Performance/StartWith:
|
155
186
|
Enabled: true
|
156
187
|
|
188
|
+
Security/Eval:
|
189
|
+
Enabled: true
|
190
|
+
|
157
191
|
Style/ArrayJoin:
|
158
192
|
Enabled: true
|
159
193
|
|
@@ -181,6 +215,9 @@ Style/ClassAndModuleCamelCase:
|
|
181
215
|
Style/ClassMethods:
|
182
216
|
Enabled: true
|
183
217
|
|
218
|
+
Style/Copyright:
|
219
|
+
Enabled: false
|
220
|
+
|
184
221
|
Style/DefWithParentheses:
|
185
222
|
Enabled: true
|
186
223
|
|
@@ -208,7 +245,7 @@ Style/InitialIndentation:
|
|
208
245
|
Style/LambdaCall:
|
209
246
|
Enabled: true
|
210
247
|
|
211
|
-
Style/
|
248
|
+
Style/MethodCallWithoutArgsParentheses:
|
212
249
|
Enabled: true
|
213
250
|
|
214
251
|
Style/MethodDefParentheses:
|
data/config/rails.yml
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubocop-github
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- GitHub
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-01-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rubocop
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '0.
|
19
|
+
version: '0.47'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '0.
|
26
|
+
version: '0.47'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: minitest
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -60,6 +60,7 @@ extra_rdoc_files: []
|
|
60
60
|
files:
|
61
61
|
- LICENSE
|
62
62
|
- README.md
|
63
|
+
- STYLEGUIDE.md
|
63
64
|
- config/default.yml
|
64
65
|
- config/rails.yml
|
65
66
|
- lib/rubocop/cop/github.rb
|