From: Mark Probert Date: 2002-04-13T04:05:40+09:00 Subject: confused about threads Hi, again. I am a little confused about threads and scope. Here is a simple version of what I want to do (the real version involves getting data via a telnet session). There is a class that gathers data (Bob) and I want it to go into an array for post processing. So there is a method load() that takes the array and puts some data in. It works okay by itself. However, when I run this with threading, the out[] has gets its keys, but the arrays are empty. I am doing something wrong. Can anyone help me out here? (The example is modified from the Pick Axe book... thanks Dave and Andy) ------- class Bob def load(n,arr) n.times do arr.push(n) end end end count = 0 arr = Array.new out = Hash.new 10.times do |i| out[i] = Array.new arr[i] = Thread.new { sleep(rand(0)/10.0) b = Bob.new b.load(count,out[i]) } end arr.each { |t| t.join } out.keys.each { |v| puts "#{v} --> #{out[v]}" } --------------------- [PROBERTM-2] ruby > irb -r threads.rb 5 --> 0 --> 6 --> 1 --> 7 --> 2 --> 8 --> 3 --> 9 --> 4 --> irb(main):001:0> h = {} {} irb(main):002:0> h[3] = Array.new [] irb(main):003:0> b = Bob.new # irb(main):004:0> b.load(3,h[3]) 3 irb(main):005:0> h {3=>[3, 3, 3]} irb(main):006:0> exit [PROBERTM-2] ruby > ruby -v ruby 1.6.6 (2001-12-26) [i686-cygwin] TIA, -mark.