I''m looking at the documentation: http://ruby-doc.org/stdlib-2.0/libdoc/net/http/rdoc/Net/HTTP.html And I notice two uses of Net::HTTP. Both are using GET requests. But what''s the difference? When to use which? Net::HTTP.start(uri.host, uri.port) do |http| request = Net::HTTP::Get.new uri response = http.request request # Net::HTTPResponse object end uri = URI(''http://example.com/index.html'') res = Net::HTTP.get_response(uri) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Tue, May 7, 2013 at 12:43 PM, John Merlino <stoicism1-YDxpq3io04c@public.gmane.org> wrote:> I''m looking at the documentation: > http://ruby-doc.org/stdlib-2.0/libdoc/net/http/rdoc/Net/HTTP.html > > And I notice two uses of Net::HTTP. Both are using GET requests. But > what''s the difference? When to use which? > > Net::HTTP.start(uri.host, uri.port) do |http| > request = Net::HTTP::Get.new uri > > response = http.request request # Net::HTTPResponse object > end > > > > uri = URI(''http://example.com/index.html'') > res = Net::HTTP.get_response(uri)I''m not sure there is a definitive answer, but my heuristic is that if I''m going to do a few things with the connection, I''ll use the first form (send code into the block, as this ensures the connection shuts down at the end of the block. If I''m only getting one thing and then carrying on with it with no need for using that connection again, I''ll use the second form. But again, this is only a heuristic, and not definitive. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Tuesday, May 7, 2013 6:43:32 PM UTC+1, John Merlino wrote:> > I''m looking at the documentation: > http://ruby-doc.org/stdlib-2.0/libdoc/net/http/rdoc/Net/HTTP.html > > And I notice two uses of Net::HTTP. Both are using GET requests. But > what''s the difference? When to use which? > >It''s the usual tradeoff between convenience and extensiveness. The long form allows you to pipeline requests, set more options on the request (eg headers etc.) but a lot of the time you just want to grab the content from a url and don''t need all the ceremony Fred> Net::HTTP.start(uri.host, uri.port) do |http| > request = Net::HTTP::Get.new uri > > response = http.request request # Net::HTTPResponse object > end > > > > uri = URI(''http://example.com/index.html'') > res = Net::HTTP.get_response(uri) >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/9GrWPdSlihMJ. For more options, visit https://groups.google.com/groups/opt_out.