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 8063435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:02:04+00:00 2026-06-05T11:02:04+00:00

What This Question Is Not About This question is not about how to auto-close

  • 0

What This Question Is Not About

This question is not about how to auto-close a file with File#close or the File#open block syntax. It’s a question about where Ruby stores its list of open file descriptors at runtime.

The Actual Question

If you have a program with open descriptors, but you don’t have access to the related File or IO object, how can you find a reference to the currently-open file descriptors? Take this example:

filename='/tmp/foo'
%x( touch "#{filename}" )
File.open(filename)
filehandle = File.open(filename)

The first File instance is opened, but the reference to the object is not stored in a variable. The second instance is stored in filehandle, where I can easily access it with #inspect or #close.

However, the discarded File object isn’t gone; it’s just not accessible in any obvious way. Until the object is finalized, Ruby must be keeping track of it somewhere…but where?

  • 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-05T11:02:06+00:00Added an answer on June 5, 2026 at 11:02 am

    TL; DR

    All File and IO objects are stored in ObjectSpace.

    Answer

    The ObjectSpace class says:

    The ObjectSpace module contains a number of routines that interact with the garbage collection facility and allow you to traverse all living objects with an iterator.

    How I Tested This

    I tested this at the console on Ruby 1.9.3p194.

    The test fixture is really simple. The idea is to have two File objects with different object identities, but only one is directly accessible through a variable. The other is “out there somewhere.”

    # Don't save a reference to the first object.
    filename='/tmp/foo'
    File.open(filename)
    filehandle = File.open(filename)
    

    I then explored different ways I could interact with the File objects even if I didn’t use an explicit object reference. This was surprisingly easy once I knew about ObjectSpace.

    # List all open File objects.
    ObjectSpace.each_object(File) do |f|
      puts "%s: %d" % [f.path, f.fileno] unless f.closed?
    end
    
    # List the "dangling" File object which we didn't store in a variable.
    ObjectSpace.each_object(File) do |f|
      unless f.closed?  
        printf "%s: %d\n", f.path, f.fileno unless f === filehandle
      end
    end
    
    # Close any dangling File objects. Ignore already-closed files, and leave
    # the "accessible" object stored in *filehandle* alone.
    ObjectSpace.each_object(File) {|f| f.close unless f === filehandle rescue nil}
    

    Conclusion

    There may be other ways to do this, but this is the answer I came up with to scratch my own itch. If you know a better way, please post another answer. The world will be a better place for it.

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

Sidebar

Related Questions

I do not know how to classify this question. Vaguely, its about using calculated
This question is not about which is the best, it is about which makes
This question is not about 'best' barcode library recommendation, we use various products on
(note that this question is not about CAS, it's about the May fail spuriously
For starters, this question is not so much about programming in the NetBeans IDE
This question calls for "line numbers". If you do not care about line numbers
Not much to say about this question...
This is not a question about a specific framework. I am using plain php
This is not a question about jQuery, but about how jQuery implements such a
Disclaimer: this is not a question about how to install asp.net or an application

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.