Skip to content

Instantly share code, notes, and snippets.

@laurenachoo
Created May 4, 2015 20:42
Show Gist options
  • Save laurenachoo/7718d5706c866fc9371a to your computer and use it in GitHub Desktop.
Save laurenachoo/7718d5706c866fc9371a to your computer and use it in GitHub Desktop.
File I/O Exercise
require 'open-uri'
url = "http://ruby.bastardsbook.com/files/fundamentals/hamlet.txt"
local_fname = "hamlet.txt"
File.open(local_fname, "w") {|file| file.write(open(url).read)}
File.open(local_fname, "r") do |file|
file.readlines.each_with_index do |line, idx|
puts line if idx % 42 == 41
end
end
require 'rubygems'
require 'rest-client'
wiki_url = "http://en.wikipedia.org/"
wiki_local_filename = "wiki-page.html"
File.open(wiki_local_filename, "w") do |file|
file.write(RestClient.get(wiki_url))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment