Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
4-line Ruby script that uses open-uri to fetch the contents of a URL & displays it in the console. This is the foundation for all web requests, whether to scrape a page, request a JSON response, and more.
# http://ruby-doc.org/stdlib-2.0.0/libdoc/open-uri/rdoc/OpenURI.html
require 'open-uri'
# Go fetch the contents of a URL & store them as a String
response = open('http://www.example.com').read
# "Pretty prints" the result to look like a web page instead of one long string of HTML
URI.parse(response).class
# Print the contents of the website to the console
puts "\n\n#{response.inspect}\n\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.