class method · added in Ruby 3.3
Dir.for_fd
(::int) -> ::Dir
Dir.for_fd(fd) → dir
dir.c
View on GitHub ↗
static VALUE
dir_s_for_fd(VALUE klass, VALUE fd)
{
struct dir_data *dp;
VALUE dir = TypedData_Make_Struct(klass, struct dir_data, &dir_data_type, dp);
if (!(dp->dir = IO_WITHOUT_GVL(nogvl_fdopendir, (void *)(VALUE)NUM2INT(fd)))) {
rb_sys_fail("fdopendir");
UNREACHABLE_RETURN(Qnil);
}
RB_OBJ_WRITE(dir, &dp->path, Qnil);
return dir;
}
Returns a new Dir object representing the directory specified by the given integer directory file descriptor fd:
Example
d0 = Dir.new('..') d1 = Dir.for_fd(d0.fileno)
Note that the returned d1 does not have an associated path:
Example
d0.path # => '..' d1.path # => nil
This method uses the fdopendir() function defined by POSIX 2008; the method is not implemented on non-POSIX platforms (raises NotImplementedError).
Ruby version changes
-
3.3
-
Dir.for_fdadded for returning aDirobject for the directory specified by the provided directory file descriptor. [Feature #19347]
-