Edited
I originally asked about doing this in one line; I meant to say one statement. I’ve edited the question accordingly.
Suppose I have a method that returns a two-item array, like a color and a day of the week.
def make_tuple
[['blue', 'red', 'orange'].sample, ['Mon','Wed', 'Fri'].sample]
end
make_tuple # => ['orange', 'Wed']
I can capture the output in two separate variables like this:
color, day = make_tuple
I can gather those outputs into two separate collections like this:
colors ||= []
days ||= []
colors << color
days << day
Is there a way to combine these two steps – getting separate values from the tuple and appending them to separate existing collections – into one statement? For instance:
# Doesn't work
colors <<, days << = make_tuple
Some examples, both functional and imperative style: