From: 7stud -- Date: 2008-01-28T18:58:49+09:00 Subject: Re: Extracting Data from a Webpage Joseph Pecoraro wrote: > 7stud -- wrote: >> Nice. I tested your code and it works for me. But my reading of the >> docs says that it shouldn't work: new() doesn't open a connection, and >> get(), "Gets data from path on the connected-to host." The docs seem >> to want you to do something like: >> >> resp_obj = Net::HTTP.get_response('http://www.google.com', >> '/index.html') >> page = resp_obj.body > > Reading the Net::HTTP docs here: > http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTP.html#M001127 > > It says that Net::HTTP#get will: > "Send a GET request to the target and return the response as a string" > > and Net::HTTP#get_response will: > "Send a GET request to the target and return the response as a > Net::HTTPResponse object" > > The #new in this case is optional because both methods are class methods > or instance methods? According to the docs, Net::HTTP has class methods: get() get_response() and an instance method: get() As with all ruby classes, new() creates an instance. Therefore, in the code example I was wondering about: > puts Net::HTTP.new('www.google.com').get('/'). > body[/(.*?)<.title>/i,1] new() creates an instance, which is being used to call get(), so the version of get() being called is the instance method. Yet, the docs say the get() instance method "Gets data from path on the connected-to host". What connected to host? According to the docs on new() it says, "This method does not open the TCP connection." In addition, the get() version in that code cannot be the class method version because the class method version returns a String and Strings do not have a body() method, which is the next method call. > Someone might be able to clarify this a part a > little more. But the examples at that doc url don't even use > New::HTTP#new. -- Posted via http://www.ruby-forum.com/.