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

  • Home
  • SEARCH
  • 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 6145089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:44:45+00:00 2026-05-23T18:44:45+00:00

This is driving me crazy. Consider the following: require ‘open-uri’ #set up tempfile extname

  • 0

This is driving me crazy. Consider the following:

require 'open-uri'

#set up tempfile
extname = File.extname file_url
basename = File.basename(file_url, extname)
file = Tempfile.new([basename,extname])

#read form URI into tempfile
uri = URI.parse(file_url)
num_bytes_writen = file.write(uri.read)
puts "Wrote #{num_bytes_writen} bytes"

# Reading from my tempfile
puts "Opening: #{file.path} >>"
puts "#### BEGINING OF FILE ####"
puts  File.open(file.path,'rb').read
puts "#### END OF FILE ####"

It looks like bytes get written, but when I try to open the file — its empty. Whats up ?!

And to make it more weird — everyting works in the Rails Console, but not when executed by a worker triggered by Resque.

Any ideas? Thanks guys

  • 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-05-23T18:44:46+00:00Added an answer on May 23, 2026 at 6:44 pm

    This is a problem of buffering. You need to flush the IO buffer to disk before trying to read it. Either file.close (if you’ve finished with it) or file.flush before doing the File.open for the read.

    Update

    I hadn’t thought about this, but you don’t need to reopen the temp file just to read it. It’s already open for writing and reading, all you need to do is seek to the start of the file before reading. This way you don’t have to do the flush (because you’re actually reading from the buffer)…

    # starting partway into your code...
    num_bytes_written = file.write(uri.read)
    puts "Wrote #{num_bytes_written} bytes"
    
    puts "No need to open #{file.path} >>"
    puts "### BEGINNING OF FILE ###"
    
    file.rewind         # set the cursor to the start of the buffer
    puts file.read      # cursor is back at the end of the buffer now
    
    puts "### END OF FILE ###"
    

    Another Update

    After a comment from @carp I have adjusted the code above to use rewind instead of seek 0 because it also resets lineno to 0 (and not having that done, if you were using lineno would be very confusing). Also actually it’s a more expressive method name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is driving me crazy. I have this one php file on a test
This has been driving me crazy for a few days. Why doesn't the following
This is driving me crazy. I have the following string in a ASP.NET 2.0
This is driving me crazy. When I set an appropriate size for my window
This is driving me crazy, as it's very easy on gitk (just gitk the/file
This is driving me crazy. I'm not going to take it anymore. I'm going
This is driving me crazy and has resulted in lost work (not much, at
This is driving me crazy, so... When resizing image to small thumbnail, resulting image
This is driving me crazy. I have a very simple user control: public int?
This is driving me crazy. Hopefully my question makes sense... I'm using MVC 2

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.