From: Roger Pack Date: 2010-07-07T01:15:19+09:00 Subject: Re: Problem of share variable in function using thread > threads = [] > numberlist = [1, 2, 3] > for id in numberlist > threads << Thread.new {testfunc(id)} > end ... > The fact is that I would like this output : > id 1 is 6 > id 2 is 7 > id 3 is 8 what's happening is that "id" is being changed as it iterates through the numberlist. So when each thread starts and finally gets around to running "testfunc(id)" they all call it with the end value. The fix is that you can pass a value "through" to the new threads, thus: Thread.new(id) {|id| testfunc(id) } Then it will preserve the original value. -r -- Posted via http://www.ruby-forum.com/.