||= for arrays

Dear messageboard users
I am just wandering if there is any operator similar to the ||= for
arrays?

#DOES NOT WORK
test_array = []
test_array ||= ['nothing...']

#DOES NOT WORK EITHER
test_array = []
test_array ||<< ['nothing...']

You could write something like this:
test_array << ['nothing...'] if test_array.size == 0

But that just doesn't look half as good as any of the two above in my
opinion.

Best regards
Sebastian Probst Eide

The ||= operator checks that the variable in question evaluates to nil
(or false, I think).

So, its behaving exactly as expected, since an empty array is not nil or false.

-ryan

You could write something like this:
test_array << ['nothing...'] if test_array.size == 0

You could do:

array = []

=> []

array << 'nothing' if array.empty?

=> ["nothing"]

array << 'something' if array.empty?

=> nil

array

=> ["nothing"]

test_array |= ['nothing...']

Array#| is the union operator.

Michael

Hi --

Thanks for all the answers!

Best regards
Sebastian