if exp1
a = exp1
elsif exp2
a = exp2
end
Can be shortened to
a = if exp1
exp1
elsif exp2
exp2
end
Or, if you prefer one-liners:
a = if exp1 then exp1 elsif exp2 then exp2 end
Any attempt to shorten it even further will change the semantics. For example:
a = exp1 || exp2 || nil
will evaluate exp1
exactly once and exp2
at most once, whereas the original snippet will evaluate exp1
once or twice and exp2
either twice or never.
(To be fair: my example will also change the meaning IFF a
appears in exp1
. In the OP's original code, an occurrence of a
in exp1
will be interpreted as a method call, in my example as a local variable dereference which will evaluate to nil
.)