Skip to content

Commit 7f60efe

Browse files
author
Zachary Jones
committed
organize and label sub-sections of syntax
2 parents ca45b25 + bdde52a commit 7f60efe

File tree

2 files changed

+90
-71
lines changed

2 files changed

+90
-71
lines changed

CONTRIBUTING.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
* [Fork](https://help.github.com/articles/fork-a-repo) the project on GitHub.
2+
* Make your feature addition or bug fix in a feature branch. (Include a description of your changes)
3+
* Push your feature branch to GitHub.
4+
* Send a [Pull Request](https://help.github.com/articles/using-pull-requests).

README.md

Lines changed: 86 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -219,18 +219,8 @@ You can generate a PDF or an HTML copy of this guide using
219219

220220
## Syntax
221221

222-
* Use `def` with parentheses when there are arguments. Omit the
223-
parentheses when the method doesn't accept any arguments.
224222

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
234224

235225
* Never use `for`, unless you know exactly why. Most of the time iterators
236226
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
249239
# good
250240
arr.each { |elem| puts elem }
251241
```
242+
243+
### Comparisons
244+
252245
* Avoid comparing booleans to boolean constants.
253246

254247
```Ruby
@@ -267,6 +260,8 @@ You can generate a PDF or an HTML copy of this guide using
267260
puts "Loading…" unless options[:silent]
268261
```
269262

263+
### If blocks
264+
270265
* Never use `then` for multi-line `if/unless`.
271266

272267
```Ruby
@@ -411,27 +406,7 @@ You can generate a PDF or an HTML copy of this guide using
411406
end
412407
```
413408

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
435410

436411
* Prefer `{...}` over `do...end` for single-line blocks. Avoid using
437412
`{...}` 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
463438
ask themselves - it this code really readable and can't the blocks contents be extracted into
464439
nifty methods.
465440
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+
466500
* Avoid `return` where not required.
467501

468502
```Ruby
@@ -494,6 +528,26 @@ You can generate a PDF or an HTML copy of this guide using
494528
While several Ruby books suggest the first style, the second is much more prominent
495529
in practice (and arguably a bit more readable).
496530

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+
497551
* Avoid line continuation (\\) where not required. In practice, avoid using
498552
line continuations at all.
499553

@@ -543,23 +597,6 @@ would happen if the current value happened to be `false`.)
543597
etc. ). They are quite cryptic and their use in anything but
544598
one-liner scripts is discouraged.
545599
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-
563600
* When the keys of your hash are symbols use the Ruby 1.9 hash literal
564601
syntax.
565602
@@ -571,28 +608,6 @@ syntax.
571608
hash = { one: 1, two: 2 }
572609
```
573610
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-
596611
## Naming
597612
598613
> The only real difficulties in programming are cache invalidation and

0 commit comments

Comments
 (0)