3

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?

James Donnelly
  • 126,410
  • 34
  • 208
  • 218
Rubytastic
  • 15,001
  • 18
  • 87
  • 175
  • possible duplicate of [How do I use the conditional operator (? :) in Ruby?](http://stackoverflow.com/questions/4252936/how-do-i-use-the-conditional-operator-in-ruby) – Rob Hruska May 29 '13 at 09:37

2 Answers2

8

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
apneadiving
  • 114,565
  • 26
  • 219
  • 213
0

Following previous answer, but using return indeed:

return foo ? false : true