The ?:
is called a ternary operator. It's not specific to Ruby; a lot of languages have it (in fact, I believe I linked you to that wikipedia page in a comment very recently).
Go easy on the newlines; there's a lot of whitespace there (update: question was edited; now it's gone)
Use string interpolation
We've talked about that is_
prefix on ?
methods already :)
To answer your question: Ternaries have their use, but like everything else, they can also be abused.
In practice, there's no difference between a ternary and and a good ol' if...else
, so you can use either - but that argument goes both ways. Don't use ternaries just because you can.
A good case for a ternary (in my opinion), would be something like this (just as an example):
puts some_boolean_value ? "yes" : "no"
or something like
"You have #{messages.count} new #{messages.one? ? 'message' : 'messages'}"
There's no logic going on in either the if
or the else
branch, and both are very short, so it's short and sweet to use a ternary.
In Ruby you can also spell the first one out like:
puts if some_boolean_value
"yes"
else
"no"
end
It's neat that Ruby treats if...else
as an expression, but here it seems like a waste of space for something so simple.
In your case: Your code does have more branching logic, so I'd definitely avoid ternaries, and keep it as-is. Personally, I'd never nest ternary expressions inside each other, just for readability reasons.
Alternatively, you can return early (assuming this code is in a method):
return information unless information.is_empty?
if leader? || possessor?
link_to("update #{contextual "your"} credentials", edit_user_path)
else
"#{user.name} hasn't entered this info!"
end
If you wrote it all in ternaries, it'd be pretty ridiculous:
information.is_empty? ? leader? || professor? ? link_to("update #{contextual "your"} credentials", edit_user_path) : "#{user.name} hasn't entered this info!" : information # what?
The middle road would be to use a ternary for the innermost nested branch only
if information.is_empty?
leader? || professor? ? link_to("update #{contextual "your"} credentials", edit_user_path) : "#{user.name} hasn't entered this info!"
else
information
end
But, as you can see, that innermost nested branch is also the one with the most going on, so it's still less readable than a normal if...else