End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
class method
File.new
File.new(path, mode = 'r', perm = 0666, **opts) → file
io.c
View on GitHub ↗
static VALUE
rb_file_initialize(int argc, VALUE *argv, VALUE io)
{
if (RFILE(io)->fptr) {
rb_raise(rb_eRuntimeError, "reinitializing File");
}
if (0 < argc && argc < 3) {
VALUE fd = rb_check_to_int(argv[0]);
if (!NIL_P(fd)) {
argv[0] = fd;
return rb_io_initialize(argc, argv, io);
}
}
rb_open_file(argc, argv, io);
return io;
}
Opens the file at the given path according to the given mode; creates and returns a new File object for that file.
The new File object is buffered mode (or non-sync mode), unless filename is a tty. See IO#flush, IO#fsync, IO#fdatasync, and IO#sync=.
Argument path must be a valid file path:
Example
f = File.new('/etc/fstab') f.close f = File.new('t.txt') f.close
Optional argument mode (defaults to ‘r’) must specify a valid mode; see Access Modes:
Example
f = File.new('t.tmp', 'w') f.close f = File.new('t.tmp', File::RDONLY) f.close
Optional argument perm (defaults to 0666) must specify valid permissions see File Permissions:
Example
f = File.new('t.tmp', File::CREAT, 0644) f.close f = File.new('t.tmp', File::CREAT, 0444) f.close
Optional keyword arguments opts specify: