Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 581
Rubytastic Avatar asked Jan 28 '26 02:01

Rubytastic


1 Answers

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
like image 137
apneadiving Avatar answered Jan 30 '26 19:01

apneadiving



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!