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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:33:44+00:00 2026-05-26T10:33:44+00:00

In ruby… I have an IO object created by an external process, which I

  • 0

In ruby…

I have an IO object created by an external process, which I need to get the file name from.
However I only seem to be able to get the File descriptor (3), which is not very useful to me.

Is there a way to get the filename from this object or even to get a File Object?

I am getting the IO object from notifier. So this may be a way of getting the file path as well?

  • 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-26T10:33:45+00:00Added an answer on May 26, 2026 at 10:33 am

    There is a similar question on how to get a the filename in C, I will present here the answer to this question in a ruby way.

    Getting the filename in Linux

    Suppose io is your IO Object. The following code gives you the filename.

    File.readlink("/proc/self/fd/#{io.fileno}")
    

    This does not work for example if the file was removed after the io object was created for it. With this solution you have the filename, but not an File object.

    Getting a File object which does not know the filename

    The method IO#for_fd can create an IO and it’s subclasses for any given integer filedescriptor. Your get your File object for your fd by doing:

    File.for_fd(io.fileno)
    

    Unfortunely this File object does not know the filename.

    File.for_fd(io.fileno).path # => nil
    

    I scanned through the ruby-1.9.2 sources. There seems to be no way in pure ruby to manipulate the path after the file object was created.

    Getting a File object which does know the filename

    An extension to ruby can be created in C which first calls File#for_fd and afterwards manipulates the Files internal data structures. This sourcecode does work for ruby-1.9.2, for other versions of ruby it may has to be adjustet.

    #include "ruby.h"
    #include "ruby/io.h"
    
    VALUE file_fd_filename(VALUE self, VALUE fd, VALUE filename) {
        VALUE file= rb_funcall3(self, rb_intern("for_fd"), 1, &fd);
        rb_io_t *fptr= RFILE(rb_io_taint_check(file))->fptr;
        fptr->pathv= rb_str_dup(filename);
        return file;
    }
    
    void Init_filename() {
    
        rb_define_singleton_method(rb_cFile, "for_fd_with_filename", file_fd_filename, 2);
    
    }
    

    Now you can do after compiling:

    require "./filename"
    f= File.for_fd_with_filename(io.fileno, File.readlink("/proc/self/fd/#{io.fileno}"))
    f.path # => the filename
    

    The readlink could also be put into the File#for_fd_with_filename definiton. This examples is just to show how it works.

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

Sidebar

Related Questions

Ruby setters—whether created by (c)attr_accessor or manually—seem to be the only methods that need
Ruby, Java, and Python all have several very good libraries which allow you to
Ruby's Enumerable has a select which can select certain items from an array: parent=@sections.select
In ruby a lot of methods have the ! marker which usually means a
Ruby has two different exceptions mechanisms: Throw/Catch and Raise/Rescue. Why do we have two?
Ruby can add methods to the Number class and other core types to get
Ruby on Rails controllers will automatically convert parameters to an array if they have
Ruby Version Manager allows us to use an .rvmrc file in each project to
Ruby's File.open takes modes and options as arguments. Where do I find a complete
Ruby newbie here. I'm trying to use the Easyjour module ( http://easyjour.rubyforge.org/ ) from

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.