It seems like both of these gems perform very similar tasks. Can anyone give examples of where one gem would be more useful than the other? I don't have specific code that I'm referring to, I'm more wondering about general use cases for each gem. I know this is a short question, I will fill in the blanks upon request. Thanks.
-
If `open-uri` fits its name, it probably means it can handle other schemes than `http`. – fge May 26 '13 at 21:55
2 Answers
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.html and http://ruby-doc.org/stdlib-1.9.3//libdoc/net/http/rdoc/Net.html

- 4,606
- 22
- 18
-
2
-
10A wrapper is simply a library that uses one or more other libraries to create a nicer, higher level interface. In this specific case, OpenURI uses the Net libraries to expose a more familiar interface, i.e. that of reading from a file. – Alex Peachey May 26 '13 at 22:53
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.

- 8,418
- 11
- 50
- 72