text
stringlengths 0
444
|
|---|
else
|
something_else
|
end
|
----
|
=== Semicolon in `if` [[no-semicolon-ifs]]
|
Do not use `if x; ...`. Use the ternary operator instead.
|
[source,ruby]
|
----
|
# bad
|
result = if some_condition; something else something_else end
|
# good
|
result = some_condition ? something : something_else
|
----
|
=== `case` vs `if-else` [[case-vs-if-else]]
|
Prefer `case` over `if-elsif` when compared value is the same in each clause.
|
[source,ruby]
|
----
|
# bad
|
if status == :active
|
perform_action
|
elsif status == :inactive || status == :hibernating
|
check_timeout
|
else
|
final_action
|
end
|
# good
|
case status
|
when :active
|
perform_action
|
when :inactive, :hibernating
|
check_timeout
|
else
|
final_action
|
end
|
----
|
=== Returning Result from `if`/`case` [[use-if-case-returns]]
|
Leverage the fact that `if` and `case` are expressions which return a result.
|
[source,ruby]
|
----
|
# bad
|
if condition
|
result = x
|
else
|
result = y
|
end
|
# good
|
result =
|
if condition
|
x
|
else
|
y
|
end
|
----
|
=== One-line Cases [[one-line-cases]]
|
Use `when x then ...` for one-line cases.
|
NOTE: The alternative syntax `when x: ...` has been removed as of Ruby 1.9.
|
=== Semicolon in `when` [[no-when-semicolons]]
|
Do not use `when x; ...`. See the previous rule.
|
=== Semicolon in `in` [[no-in-pattern-semicolons]]
|
Do not use `in pattern; ...`. Use `in pattern then ...` for one-line `in` pattern branches.
|
[source,ruby]
|
----
|
# bad
|
case expression
|
in pattern; do_something
|
end
|
# good
|
case expression
|
in pattern then do_something
|
end
|
----
|
=== `!` vs `not` [[bang-not-not]]
|
Use `!` instead of `not`.
|
[source,ruby]
|
----
|
# bad - parentheses are required because of op precedence
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.