I'm trying to create some arrays in some threads and name them after a fixnum between 1 and 30 like the following:
times = 30 n=0 thread = [] while n < times thread << Thread.new(n) {|x| array#{x} = Array.new() ... } end How can I do that?
I'm trying to create some arrays in some threads and name them after a fixnum between 1 and 30 like the following:
times = 30 n=0 thread = [] while n < times thread << Thread.new(n) {|x| array#{x} = Array.new() ... } end How can I do that?
Ruby does not allow you to create variable names from strings in the same way that PHP does. In a case such as this you could use an array or a hash instead.
times = 30 n=0 arrays = [] thread = [] while n < times thread << Thread.new(n) {|x| arrays[x] = Array.new() ... } end You can also use more rubyish constructs instead of while, such as Fixnum#times.
arrays = [] threads = 30.times.map do |n| Thread.new do arrays[x] = Array.new # ... end end > arrays #=> [[], [], [], ....] > threads #=> [#<Thread:0x007fe3f22a2320 dead>, #<Thread:0x007fe3f22a2208 dead>, ...]