這個小腳本應該生成一個用戶指定數量的隨機數並打印它們。這是一個多線程腳本,我認爲這是我的麻煩所在。我沒有收到任何錯誤,但運行腳本時就掛起了。Ruby腳本永遠掛起
num = []
while 0.upto ARGV[0].to_i do
num << rand{254}
end
current_index = 0
while current_index < num.size
chunk = num[current_index, 5]
threads = []
chunk.each do |n|
threads << Thread.new do
puts n
end
end
threads.each do |thread|
thread.join
end
current_index += chunk.size
end
你的問題是什麼? – sawa 2014-10-19 00:10:54
您應該避免使用與Ruby內置方法的名稱相同的變量名稱。也許你不熟悉方法[Enumerable#chunk](http://www.ruby-doc.org/core-2.1.1/Enumerable.html#method-i-chunk)。 – 2014-10-19 01:37:30