text
stringlengths
0
444
=== Using `else` with `unless` [[no-else-with-unless]]
Do not use `unless` with `else`.
Rewrite these with the positive case first.
[source,ruby]
----
# bad
unless success?
puts 'failure'
else
puts 'success'
end
# good
if success?
puts 'success'
else
puts 'failure'
end
----
=== Parentheses around Condition [[no-parens-around-condition]]
Don't use parentheses around the condition of a control expression.
[source,ruby]
----
# bad
if (x > 10)
# body omitted
end
# good
if x > 10
# body omitted
end
----
NOTE: There is an exception to this rule, namely <<safe-assignment-in-condition,safe assignment in condition>>.
=== Multi-line `while do` [[no-multiline-while-do]]
Do not use `while/until condition do` for multi-line `while/until`.
[source,ruby]
----
# bad
while x > 5 do
# body omitted
end
until x > 5 do
# body omitted
end
# good
while x > 5
# body omitted
end
until x > 5
# body omitted
end
----
=== `while` as a Modifier [[while-as-a-modifier]]
Prefer modifier `while/until` usage when you have a single-line body.
[source,ruby]
----
# bad
while some_condition
do_something
end
# good
do_something while some_condition
----
=== `while` vs `until` [[until-for-negatives]]
Prefer `until` over `while` for negative conditions.
[source,ruby]
----
# bad
do_something while !some_condition
# good
do_something until some_condition
----
=== Infinite Loop [[infinite-loop]]
Use `Kernel#loop` instead of `while`/`until` when you need an infinite loop.
[source,ruby]