@@ -219,18 +219,8 @@ You can generate a PDF or an HTML copy of this guide using
219
219
220
220
# # Syntax
221
221
222
- * Use ` def` with parentheses when there are arguments. Omit the
223
- parentheses when the method doesn' t accept any arguments.
224
222
225
- ```Ruby
226
- def some_method
227
- # body omitted
228
- end
229
-
230
- def some_method_with_arguments(arg1, arg2)
231
- # body omitted
232
- end
233
- ```
223
+ # ## For
234
224
235
225
* Never use ` for` , unless you know exactly why. Most of the time iterators
236
226
should be used instead. ` for` is implemented in terms of ` each` (so
@@ -249,6 +239,9 @@ You can generate a PDF or an HTML copy of this guide using
249
239
# good
250
240
arr.each { |elem| puts elem }
251
241
` ` `
242
+
243
+ # ## Comparisons
244
+
252
245
* Avoid comparing booleans to boolean constants.
253
246
254
247
` ` ` Ruby
@@ -267,6 +260,8 @@ You can generate a PDF or an HTML copy of this guide using
267
260
puts "Loading…" unless options[:silent]
268
261
` ` `
269
262
263
+ # ## If blocks
264
+
270
265
* Never use ` then` for multi- line ` if/unless` .
271
266
272
267
` ` ` Ruby
@@ -411,27 +406,7 @@ You can generate a PDF or an HTML copy of this guide using
411
406
end
412
407
` ` `
413
408
414
- * Omit parentheses around parameters for methods that are part of an
415
- internal DSL (e.g. Rake, Rails, RSpec), methods that are with
416
- "keyword" status in Ruby (e.g. `attr_reader`, `puts`) and attribute
417
- access methods. Use parentheses around the arguments of all other
418
- method invocations.
419
-
420
- ```Ruby
421
- class Person
422
- attr_reader :name, :age
423
-
424
- # omitted
425
- end
426
-
427
- temperance = Person.new(' Temperance ' , 30)
428
- temperance.name
429
-
430
- puts temperance.age
431
-
432
- x = Math.sin(y)
433
- array.delete(e)
434
- ```
409
+ # ## Blocks
435
410
436
411
* Prefer ` {...}` over ` do...end` for single- line blocks. Avoid using
437
412
` {...}` for multi- line blocks (multiline chaining is always
@@ -463,6 +438,65 @@ You can generate a PDF or an HTML copy of this guide using
463
438
ask themselves - it this code really readable and can' t the blocks contents be extracted into
464
439
nifty methods.
465
440
441
+ * Use the new lambda literal syntax.
442
+
443
+ ```Ruby
444
+ # bad
445
+ lambda = lambda { |a, b| a + b }
446
+ lambda.call(1, 2)
447
+
448
+ # good
449
+ lambda = ->(a, b) { a + b }
450
+ lambda.(1, 2)
451
+ ```
452
+
453
+ * Use `_` for unused block parameters.
454
+
455
+ ```Ruby
456
+ # bad
457
+ result = hash.map { |k, v| v + 1 }
458
+
459
+ # good
460
+ result = hash.map { |_, v| v + 1 }
461
+ ```
462
+
463
+ ### Methods
464
+
465
+ * Use `def` with parentheses when there are arguments. Omit the
466
+ parentheses when the method doesn' t accept any arguments.
467
+
468
+ ` ` ` Ruby
469
+ def some_method
470
+ # body omitted
471
+ end
472
+
473
+ def some_method_with_arguments(arg1, arg2)
474
+ # body omitted
475
+ end
476
+ ` ` `
477
+
478
+ * Omit parentheses around parameters for methods that are part of an
479
+ internal DSL (e.g. Rake , Rails , RSpec ), methods that are with
480
+ " keyword" status in Ruby (e.g. ` attr_reader` , ` puts` ) and attribute
481
+ access methods. Use parentheses around the arguments of all other
482
+ method invocations.
483
+
484
+ ` ` ` Ruby
485
+ class Person
486
+ attr_reader :name, :age
487
+
488
+ # omitted
489
+ end
490
+
491
+ temperance = Person.new('Temperance', 30)
492
+ temperance.name
493
+
494
+ puts temperance.age
495
+
496
+ x = Math.sin(y)
497
+ array.delete(e)
498
+ ` ` `
499
+
466
500
* Avoid ` return` where not required.
467
501
468
502
` ` ` Ruby
@@ -494,6 +528,26 @@ You can generate a PDF or an HTML copy of this guide using
494
528
While several Ruby books suggest the first style, the second is much more prominent
495
529
in practice (and arguably a bit more readable).
496
530
531
+ * Never put a space between a method name and the opening parenthesis.
532
+
533
+ ` ` ` Ruby
534
+ # bad
535
+ f (3 + 2) + 1
536
+
537
+ # good
538
+ f(3 + 2) + 1
539
+ ` ` `
540
+
541
+ * If the first argument to a method begins with an open parenthesis,
542
+ always use parentheses in the method invocation. For example, write
543
+ ` f((3 + 2) + 1)` .
544
+
545
+ * Always run the Ruby interpreter with the ` -w` option so it will warn
546
+ you if you forget either of the rules above!
547
+
548
+
549
+ # ## Assignments
550
+
497
551
* Avoid line continuation (\\) where not required. In practice, avoid using
498
552
line continuations at all.
499
553
@@ -543,23 +597,6 @@ would happen if the current value happened to be `false`.)
543
597
etc. ). They are quite cryptic and their use in anything but
544
598
one-liner scripts is discouraged.
545
599
546
- * Never put a space between a method name and the opening parenthesis.
547
-
548
- ```Ruby
549
- # bad
550
- f (3 + 2) + 1
551
-
552
- # good
553
- f(3 + 2) + 1
554
- ```
555
-
556
- * If the first argument to a method begins with an open parenthesis,
557
- always use parentheses in the method invocation. For example, write
558
- `f((3 + 2) + 1)`.
559
-
560
- * Always run the Ruby interpreter with the `-w` option so it will warn
561
- you if you forget either of the rules above!
562
-
563
600
* When the keys of your hash are symbols use the Ruby 1.9 hash literal
564
601
syntax.
565
602
@@ -571,28 +608,6 @@ syntax.
571
608
hash = { one: 1, two: 2 }
572
609
```
573
610
574
- * Use the new lambda literal syntax.
575
-
576
- ```Ruby
577
- # bad
578
- lambda = lambda { |a, b| a + b }
579
- lambda.call(1, 2)
580
-
581
- # good
582
- lambda = ->(a, b) { a + b }
583
- lambda.(1, 2)
584
- ```
585
-
586
- * Use `_` for unused block parameters.
587
-
588
- ```Ruby
589
- # bad
590
- result = hash.map { |k, v| v + 1 }
591
-
592
- # good
593
- result = hash.map { |_, v| v + 1 }
594
- ```
595
-
596
611
## Naming
597
612
598
613
> The only real difficulties in programming are cache invalidation and
0 commit comments