File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -1953,6 +1953,51 @@ end
19531953end
19541954----
19551955
1956+ === `else` branches after loop exit [[else-after-loop-exit]]
1957+
1958+ There is no need to wrap code inside an `else` when the `if` statement skips the rest of the loop.
1959+
1960+ [source,ruby]
1961+ ----
1962+ # bad
1963+ [0, 1, 2, 3].each do |item|
1964+ if some_condition
1965+ do_something
1966+ next
1967+ else
1968+ do_something_else
1969+ end
1970+ end
1971+
1972+ [0, 1, 2, 3].each do |item|
1973+ if some_condition
1974+ do_something
1975+ break
1976+ else
1977+ do_something_else
1978+ end
1979+ end
1980+
1981+ [0, 1, 2, 3].each do |item|
1982+ if some_condition
1983+ do_something
1984+ return
1985+ else
1986+ do_something_else
1987+ end
1988+ end
1989+
1990+ # good
1991+ [0, 1, 2, 3].each do |item|
1992+ if some_condition
1993+ do_something
1994+ next
1995+ end
1996+
1997+ do_something_else
1998+ end
1999+ ----
2000+
19562001== Exceptions
19572002
19582003=== `raise` vs `fail` [[prefer-raise-over-fail]]
You can’t perform that action at this time.
0 commit comments