Reputation: 1
I know conditional assignments. If the first element evaluates to nil
the second element is taken.
irb(main):004:0> nil || 42
=> 42
Is there a similar concept for lists, such as the second path only gets evaluated if the first is empty not nil
.
irb(main):004:0> [] || [1,2,3]
=> [1, 2, 3]
Upvotes: 0
Views: 33
Reputation: 168081
Literally having
[] || [1,2,3]
is useless, so I guess you have it as a certain variable, say a
, which might happen to be []
. The best you can do is:
a.empty? ? [1, 2, 3] : a
If you are not satisfied with this, and insist along the way you tried, then you need to define additional methods.
class Array
def tweeze; self unless empty? end
end
a.tweeze || [1, 2, 3]
Upvotes: 2