This question already has an answer here:
- What is the difference between Ruby's 'open-uri' and 'Net:HTTP' gems? 2 answers
A separate SO post offers different methods for fetching web content in Ruby, but doesn't fully explain why one is preferable to the other.
What is the difference between using open() and the NET::HTTP module, as demonstrated below, to fetch web content? Why is NET::HTTP considered the "better" approach?
**open() 1:**
require 'open-uri'
file = open('http://hiscore.runescape.com/index_lite.ws?player=zezima')
contents = file.read
**open() 2:**
require 'open-uri'
source = open('http://www.google.com', &:read)
**NET::HTTP 1:**
require 'uri'
require 'net/http'
url = "http://hiscore.runescape.com/index_lite.ws?player=zezima"
r = Net::HTTP.get_response(URI.parse(url).host, URI.parse(url).path)