Skip to content

Instantly share code, notes, and snippets.

@SabretWoW
Created December 9, 2013 17:16
Show Gist options
  • Save SabretWoW/7876153 to your computer and use it in GitHub Desktop.
Save SabretWoW/7876153 to your computer and use it in GitHub Desktop.
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