Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append the same value to two or more arrays

Tags:

ruby

Is it possible to append the same value to two different arrays in one statement? For e.g.,

a = [], b = [] 
a,b << 10 
like image 453
Ankush Ganatra Avatar asked Jan 12 '23 06:01

Ankush Ganatra


1 Answers

If you want to keep different arrays then I think the only possibility is to do this:

a, b = a << 10, b << 10

Obviously, it does not fulfill the requirement to write the value just once. With the comma it is possible to write values in an array notation. On the left side of the assignment are two values which can consume an array up to length two.

What is on the right side of the assignment? There are two values written in array notation. After evaluation you could write:

a, b = [[a], [b]]

# a <- [a]
# b <- [b]

Alternatively, if you are fine with semicolon:

a << 10; b << 10;
like image 52
Konrad Reiche Avatar answered Jan 18 '23 23:01

Konrad Reiche