Comments on: Ruby script to fetch hosts file and turn it into a privoxy block list http://www.aquick.org/blog/2006/02/01/ruby-script-to-fetch-hosts-file-and-turn-it-into-a-privoxy-block-list/ entertaining hundreds of millions of eyeball atoms every day Sun, 12 Aug 2012 17:06:22 -0400 http://wordpress.org/?v=2.8.4 hourly 1 By: James Wetterau http://www.aquick.org/blog/2006/02/01/ruby-script-to-fetch-hosts-file-and-turn-it-into-a-privoxy-block-list/comment-page-1/#comment-681 James Wetterau Wed, 01 Feb 2006 23:57:37 +0000 http://www.aquick.org/blog/2006/02/01/ruby-script-to-fetch-hosts-file-and-turn-it-into-a-privoxy-block-list/#comment-681 file.rewind() file.rewind()

]]>
By: adam http://www.aquick.org/blog/2006/02/01/ruby-script-to-fetch-hosts-file-and-turn-it-into-a-privoxy-block-list/comment-page-1/#comment-680 adam Wed, 01 Feb 2006 23:46:39 +0000 http://www.aquick.org/blog/2006/02/01/ruby-script-to-fetch-hosts-file-and-turn-it-into-a-privoxy-block-list/#comment-680 Since I/O iterators maintain their position when you break out of the initial block, how do you go back to the beginning of the file if you want to reset the each_line? Do you need to close and reopen the file? Since I/O iterators maintain their position when you break out of the initial block, how do you go back to the beginning of the file if you want to reset the each_line? Do you need to close and reopen the file?

]]>
By: James Wetterau http://www.aquick.org/blog/2006/02/01/ruby-script-to-fetch-hosts-file-and-turn-it-into-a-privoxy-block-list/comment-page-1/#comment-677 James Wetterau Wed, 01 Feb 2006 22:55:04 +0000 http://www.aquick.org/blog/2006/02/01/ruby-script-to-fetch-hosts-file-and-turn-it-into-a-privoxy-block-list/#comment-677 My minor changes -- not sure if this is the ruby way or not, and I too, would be curious to hear from more experienced Ruby developers what they think of what I've done here: <pre> require 'open-uri' hosts = Array.new begin open('http://www.mvps.org/winhelp2002/hosts.txt') do |file| # skip if still in header file.each_line() { |line| break if line =~ /^#start/ } # skip comments and add the hostname to an array file.each_line() { |line| hosts << line.split[1] if line !~ /^\s*#/ } end rescue print "An error occurred reading data from the web: " + $! + "\n" exit 1 end # write the output file begin outfile = open('privoxy_user_actions.txt', "w") rescue print "Cannot open file: " + $! + "\n" exit 2 end outfile.puts "{ +block }\n" hosts.each { |host| outfile.puts host + "\n" } outfile.close </pre</pre> My minor changes — not sure if this is the ruby way or not, and I too,
would be curious to hear from more experienced Ruby developers what
they think of what I’ve done here:

require 'open-uri'

hosts = Array.new

begin
  open('http://www.mvps.org/winhelp2002/hosts.txt') do |file|
    # skip if still in header
    file.each_line() { |line| break if line =~ /^#start/ }

    # skip comments and add the hostname to an array
    file.each_line() { |line| hosts < < line.split[1] if line !~ /^\s*#/ }
  end
rescue
  print "An error occurred reading data from the web: " + $! + "\n"
  exit 1
end

# write the output file
begin
  outfile = open('privoxy_user_actions.txt', "w")
rescue
  print "Cannot open file: " + $! + "\n"
  exit 2
end
outfile.puts "{ +block }\n"
hosts.each { |host| outfile.puts host + "\n" }
outfile.close

]]>