很多朋友在初学Ruby线程时候,会遇到in `get': undefined method `keys' for nil:NilClass (NoMethodError)这个问题;
下面举一个简单的例子:
require 'net/http'
pages = %w( www.youkuaiyun.com www.rubycentral.com www.baidu.com )
threads = []
for page_to_fetch in pages
threads << Thread.new(page_to_fetch) do |url|
h = Net::HTTP.new(url,80)
puts "Fetching: #{url} "
resp = h.get('/',nil)
puts "Got #{url}: #{resp.message}"
end
end
threads.each {|thr| thr.join }
运行上述程序会报错in `get': undefined method `keys' for nil:NilClass (NoMethodError)
起始很简单,只要把resp = h.get('/',nil)改为resp = h.get('/')即可。
希望以后关于Ruby的资料会更多些。。。