From: Jerome David Sallinger Date: 2011-01-01T21:23:59+09:00 Subject: Newbie: Thread variables in Ruby Threads Take the following piece of code: ------------------------------------------------------ count = 0 arr = [] 10.times do |i| arr[i] = Thread.new { sleep(1) Thread.current["mycount"] = count count += 1 } end arr.each {|t| t.join; print t["mycount"], ", " } puts "count = #{count}" ------------------------------------------------------ It produces the following output: 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, count = 10 I dont understand how it does this. This is what I would have expected the output to have been as a result of each iteration of the #times loop. arr[0] count 0 arr[1] count 1 .. arr[9] count 9 so that when it reaches the line starting arr.each it would produce the output 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 count = 10; which is obviously the reverse of what was actually printed. What am I missing here? Thanxs in advance. -- Posted via http://www.ruby-forum.com/.