Is there a more elegant way of implementing the following logic in Ruby?
a = nil #=> obviously 'a' can have value but I am just setting it to nil to make this example clearer
b = a
unless b
b = "value"
end
Thus, we have the value of b set in the end. We could have another variation of the above code, like so:
a = nil
b = a
b ||= "value"
And I can also use ternary statement to write the above as:
b = a ? a : "value"
But, if you replace variable a and the expression "value" with a long line of code, then a ternary statement starts looking ugly, too.
Can this be made more elegant and expressive somehow, or are we limited to just the above solutions?
You almost answered your question yourself.
Namely, the two statements
can be combined into a single one: