@@ -23,8 +23,10 @@ This is GitHub's Ruby Style Guide, inspired by [RuboCop's guide][rubocop-guide].
23
23
15 . [ Methods] ( #methods )
24
24
1 . [ Method definitions] ( #method-definitions )
25
25
2 . [ Method calls] ( #method-calls )
26
-
27
- 16 . [ Syntax] ( #syntax )
26
+ 16 . [ Conditional Expressions] ( #conditional-expressions )
27
+ 1 . [ Conditional keywords] ( #conditional-keywords )
28
+ 2 . [ Ternary operator] ( #ternary-operator )
29
+ 17 . [ Syntax] ( #syntax )
28
30
29
31
## Layout
30
32
@@ -595,25 +597,9 @@ f (3 + 2) + 1
595
597
f(3 + 2 ) + 1
596
598
```
597
599
598
- ## Syntax
600
+ ## Conditional Expressions
599
601
600
- * Never use ` for ` , unless you know exactly why. Most of the time iterators
601
- should be used instead. ` for ` is implemented in terms of ` each ` (so
602
- you're adding a level of indirection), but with a twist - ` for `
603
- doesn't introduce a new scope (unlike ` each ` ) and variables defined
604
- in its block will be visible outside it.
605
-
606
- ``` ruby
607
- arr = [1 , 2 , 3 ]
608
-
609
- # bad
610
- for elem in arr do
611
- puts elem
612
- end
613
-
614
- # good
615
- arr.each { |elem | puts elem }
616
- ```
602
+ ### Conditional keywords
617
603
618
604
* Never use ` then ` for multi-line ` if/unless ` .
619
605
@@ -629,38 +615,8 @@ if some_condition
629
615
end
630
616
```
631
617
632
- * Avoid the ternary operator (` ?: ` ) except in cases where all expressions are extremely
633
- trivial. However, do use the ternary operator(` ?: ` ) over ` if/then/else/end ` constructs
634
- for single line conditionals.
635
-
636
- ``` ruby
637
- # bad
638
- result = if some_condition then something else something_else end
639
-
640
- # good
641
- result = some_condition ? something : something_else
642
- ```
643
-
644
- * Use one expression per branch in a ternary operator. This
645
- also means that ternary operators must not be nested. Prefer
646
- ` if/else ` constructs in these cases.
647
-
648
- ``` ruby
649
- # bad
650
- some_condition ? (nested_condition ? nested_something : nested_something_else) : something_else
651
-
652
- # good
653
- if some_condition
654
- nested_condition ? nested_something : nested_something_else
655
- else
656
- something_else
657
- end
658
- ```
659
-
660
618
* The ` and ` and ` or ` keywords are banned. It's just not worth it. Always use ` && ` and ` || ` instead.
661
619
662
- * Avoid multi-line ` ?: ` (the ternary operator), use ` if/unless ` instead.
663
-
664
620
* Favor modifier ` if/unless ` usage when you have a single-line
665
621
body.
666
622
@@ -706,6 +662,58 @@ if x > 10
706
662
end
707
663
```
708
664
665
+ ### Ternary operator
666
+
667
+ * Avoid the ternary operator (` ?: ` ) except in cases where all expressions are extremely
668
+ trivial. However, do use the ternary operator(` ?: ` ) over ` if/then/else/end ` constructs
669
+ for single line conditionals.
670
+
671
+ ``` ruby
672
+ # bad
673
+ result = if some_condition then something else something_else end
674
+
675
+ # good
676
+ result = some_condition ? something : something_else
677
+ ```
678
+
679
+ * Avoid multi-line ` ?: ` (the ternary operator), use ` if/unless ` instead.
680
+
681
+ * Use one expression per branch in a ternary operator. This
682
+ also means that ternary operators must not be nested. Prefer
683
+ ` if/else ` constructs in these cases.
684
+
685
+ ``` ruby
686
+ # bad
687
+ some_condition ? (nested_condition ? nested_something : nested_something_else) : something_else
688
+
689
+ # good
690
+ if some_condition
691
+ nested_condition ? nested_something : nested_something_else
692
+ else
693
+ something_else
694
+ end
695
+ ```
696
+
697
+ ## Syntax
698
+
699
+ * Never use ` for ` , unless you know exactly why. Most of the time iterators
700
+ should be used instead. ` for ` is implemented in terms of ` each ` (so
701
+ you're adding a level of indirection), but with a twist - ` for `
702
+ doesn't introduce a new scope (unlike ` each ` ) and variables defined
703
+ in its block will be visible outside it.
704
+
705
+ ``` ruby
706
+ arr = [1 , 2 , 3 ]
707
+
708
+ # bad
709
+ for elem in arr do
710
+ puts elem
711
+ end
712
+
713
+ # good
714
+ arr.each { |elem | puts elem }
715
+ ```
716
+
709
717
* Prefer ` {...} ` over ` do...end ` for single-line blocks. Avoid using
710
718
` {...} ` for multi-line blocks (multiline chaining is always
711
719
ugly). Always use ` do...end ` for "control flow" and "method
0 commit comments