End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
class method
File.open
File.open(filename, mode="r" [, opt]) → file
File.open(filename [, mode [, perm]] [, opt]) → file
File.open(filename, mode="r" [, opt]) {|file| block } → obj
File.open(filename [, mode [, perm]] [, opt]) {|file| block } → obj
io.c
View on GitHub ↗
static VALUE
rb_io_s_open(int argc, VALUE *argv, VALUE klass)
{
VALUE io = rb_class_new_instance_kw(argc, argv, klass, RB_PASS_CALLED_KEYWORDS);
if (rb_block_given_p()) {
return rb_ensure(rb_yield, io, io_close, io);
}
return io;
}
With no associated block, File.open is a synonym for File.new. If the optional code block is given, it will be passed the opened file as an argument and the File object will automatically be closed when the block terminates. The value of the block will be returned from File.open.
If a file is being created, its initial permissions may be set using the perm parameter. See File.new for further discussion.
See IO.new for a description of the mode and opt parameters.