Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8995519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:37:51+00:00 2026-06-15T23:37:51+00:00

I have a Ruby script (1.9.2p290) where I am trying to call a number

  • 0

I have a Ruby script (1.9.2p290) where I am trying to call a number of URLs, and then append information from those URLs into a file. The issue is that I keep getting an end of file error – EOFError. An example of what I’m trying to do is:

require "open-uri"
proxy_uri = URI.parse("http://IP:PORT")
somefile = File.open("outputlist.txt", 'a')

(1..100).each do |num|
  page = open('SOMEURL' + num, :proxy => proxy_uri).read
  pattern = "<img"   
  tags = page.scan(pattern)
  output << tags.length
end
somefile.puts output
somefile.close

I don’t know why I keep getting this end of file error, or how I can avoid getting the error. I think it might have something to do with the URL that I’m calling (based on some dialogue here: What is an EOFError in Ruby file I/O?), but I’m not sure why that would affect the I/O or cause an end of file error.

Any thoughts on what I might be doing wrong here or how I can get this to work?

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:37:52+00:00Added an answer on June 15, 2026 at 11:37 pm

    The way you are writing your file isn’t idiomatic Ruby. This should work better:

    (1..100).each do |num|
      page = open('SOMEURL' + num, :proxy => proxy_uri).read
      pattern = "<img"   
      tags = page.scan(pattern)
      output << tags.length
    end
    
    File.open("outputlist.txt", 'a') do |fo|
      fo.puts output
    end
    

    I suspect that the file is being closed because it’s been opened, then not written-to while 100 pages are processed. If that takes a while I can see why they’d close it to avoid apps using up all the file handles. Writing it the Ruby-way automatically closes the file immediately after the write, avoiding holding handles open artificially.

    As a secondary thing, rather than use a simple pattern match to try to locate image tags, use a real HTML parser. There will be little difference in processing speed, but potentially more accuracy.

    Replace:

    page = open('SOMEURL' + num, :proxy => proxy_uri).read
    pattern = "<img"   
    tags = page.scan(pattern)
    output << tags.length
    

    with:

    require 'nokogiri'
    
    doc = Nokogiri::HTML(open('SOMEURL' + num, :proxy => proxy_uri))
    output << doc.search('img').size
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ruby script that downloads URLs from an RSS server and then
I have rails website that shows information from database. And I have ruby script
I have a ruby script which will read data from some CSV file and
so i have ruby script that simply writes to a test.txt file with hello
I have a ruby bash script to download a zip file and output a
I have two ruby script cron jobs that I'm trying to run under Ubuntu
I have a Ruby script that generates an ANSI file. I want to convert
I have a ruby script that loops through a list of shortened urls (around
I have a ruby script below which infinitely prints numbers from 1 onward. How
I have a ruby script I'm trying to test with rspec. Is there a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.