I need to do something like the following
e = []
m = []
p = []
t = []
...and so on for about 10 different arrays
Is there a way to create all these arrays on one line?
You can do it using the following:
e,m,p,t... = Array.new(10) { [] }
It turns out
[[]]*10
is not the right way to go, [[]]*10
is for repetition and is just repeating the same object []
10 times, so all the variables would end up getting assigned to the same object.
I'm curious at what are those 10 different arrays, because I would suspect they shouldn't be 10 different variables but just one. You don't give any context, so I can only guess, something like the following might better:
whatever = Hash.new{|h, k| h[k] = []}
whatever[:e] # => []
whatever[:m] << 42
whatever[:m] # => [42]
# etc...
Otherwise, as zomboid wrote:
e, m, p, t ... = [], [], [], [] ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With