What is the difference between Ruby's 'open-uri' and 'Net:HTTP' gems? What is the difference between Ruby's 'open-uri' and 'Net:HTTP' gems? ruby ruby

What is the difference between Ruby's 'open-uri' and 'Net:HTTP' gems?


The reason they look like they perform similar tasks is OpenURI is a wrapper for Net::HTTP, Net::HTTPS, and Net::FTP.

Usually, unless you feel you need a lower level interface, using OpenURI is better as you can get by with less code. Using OpenURI you can open a URL/URI and treat it as a file.

See: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/open-uri/rdoc/OpenURI.htmland http://ruby-doc.org/stdlib-1.9.3//libdoc/net/http/rdoc/Net.html


I just found out that open does follow redirections, while Net::HTTP doesn't, which is an important difference.

For example, open('http://www.stackoverflow.com') { |content| puts content.read } will display the proper HTML after following the redirection, while Net::HTTP.get(URI('http://www.stackoverflow.com')) will show the redirection message and 302 status code.