The same as with the original code. That it is in one line, doesn’t
change the issue.
It’s a question of how we communicate the intent of our goal to the
reader. For example,
z = c ? a : b
communicates immediately, that z is assigned a value, and WHICH value is
being assigned, depends on c. Therefore I consider it more clearly than
if c
z = a
else
z = b
end
The “clearliness” doesn’t come from the fact that the first version is
shorter (less to type), but because the reader immediately sees in the
first case, that this piece of code changes z, while in the second case,
he has to examine two lines and deduct that in both cases, the same
variable will be changed. While this is easy here (having one-letter
variable names), we can imagine well that z could be a much longer name
(or expression), where we have to look more carefully, whether both
targets of the assignment are the same.
Similarily,
if c
bee.bop.boo=foo.bar.baz{n]
else
xyz=foo.bar.baz[n]
end
is not that much readable, considering a hypothetical construct
(c ? bee.bop.boo : xyz) = foo.bar.baz{n]
Actually, I would even consider introducing a temporary variable to make
it more readable in this case:
t=foo.bar.baz{n]
if c
bee.bop.boo=t
else
xyz=t
end
The reason, why I posted this, was not to make a suggestion for a change
to the Ruby language (this would probably the wrong forum), but because
from my experience, Ruby is designed to make everything easy and simple,
and I thought that, maybe, there IS a concise way to express such a
conditional in Ruby, and I just don’t know it. Well, maybe there really
is none…