Мультипотоковый web-серфинг на Ruby


#!/usr/bin/env ruby
# -*- coding: UTF-8 -*-

require 'net/http'
require 'uri'

n = %w(1 2 3 4 5 6 7 8 9)

threads = []

c = 1
for i in n
    threads << Thread.new(i) { |str|
        # set thread variable
        Thread.current["counter"] = c
        url = URI.parse('http://www.test.ru')
        begin
            xx = (65+Random.rand(26)).chr << (65+Random.rand(26)).chr
            u1 = "/search/?search_string="<<xx<<"&search_type=all"
            puts u1
            res = Net::HTTP.start(url.host, url.port) { |http|
                http.get(u1)
            }
        rescue
            sleep(1)
        end
        puts ""<<res.code
    }
    c += 1
    puts c
end

# wait for threads
threads.each { |x|
    x.join
    #puts "Done "<< x["counter"].to_s
}