One thing I love about Ruby is that you can express things in the shortest way possible.
I know that one can do, when assigning
x ||= a
# instead of
x = a unless x
# which is
x = x || a
Is there an analog form for return?
# instead of
return x if x
I’m trying to “say” x only once. This question asks about just returning (nothing), but I don’t see how to do it when returning something other than void.
I’m just about certain that there exists no shorthand for your second example—nor could one be written without modifying the Ruby syntax—since it’s not a common enough idiom. Sorry, bro, but it looks like you’re going to have to be verbose on this one. (Though, really, as far as verbosity goes, this one isn’t all that bad.)
(Note, too, that the first example isn’t quite right:
x ||= ais equivalent tox = x || a, which can also be expressed asx = a unless x.)