Rubytastic
Rubytastic

Reputation: 15491

refactor rails if else statement with to a single line with return?

Im refactoring my older code parts, have lots of returns with multi line like:

if ...
 return false
else
 return true
end

How could one refactor to use a single line and return true or false?

Upvotes: 3

Views: 6771

Answers (2)

Fabián Contreras
Fabián Contreras

Reputation: 43

Following previous answer, but using return indeed:

return foo ? false : true

Upvotes: 0

apneadiving
apneadiving

Reputation: 115521

Say foo is what is on the right of your if, then you can replace with:

foo ? false : true

This is known as the ternary operator.

Notice that in your case you could simply do:

!foo

Upvotes: 8

Related Questions