J
JB
I wrote this program
threads = []
for i in 1..3
threads << Thread.new(i) { |i|
sleep(i)
print i
}
end
threads.each { |aThread| aThread.join }
I expected to see: 123
but I got : 333
Is it a bug? or is it normal?
To my mind it might be a bug because the code doesn't do what it seems to
do.(?)
Upon to me the 'i' variable of the 'print i' should be '|i|' (which is the
last declaration of 'i') and not the 'i' in 'for i in 1..3' (which value is
'3' and the end of the loop).
Can anybody explain me?
Thanks
Ps:
Of course if I write
for i in 1..3
threads << Thread.new(i) { |a|
sleep(1)
print a
}
it works.
threads = []
for i in 1..3
threads << Thread.new(i) { |i|
sleep(i)
print i
}
end
threads.each { |aThread| aThread.join }
I expected to see: 123
but I got : 333
Is it a bug? or is it normal?
To my mind it might be a bug because the code doesn't do what it seems to
do.(?)
Upon to me the 'i' variable of the 'print i' should be '|i|' (which is the
last declaration of 'i') and not the 'i' in 'for i in 1..3' (which value is
'3' and the end of the loop).
Can anybody explain me?
Thanks
Ps:
Of course if I write
for i in 1..3
threads << Thread.new(i) { |a|
sleep(1)
print a
}
it works.