instance method
IO#close
() -> nil
close → nil
io.c
View on GitHub ↗
static VALUE
rb_io_close_m(VALUE io)
{
rb_io_t *fptr = rb_io_get_fptr(io);
if (fptr->fd < 0) {
return Qnil;
}
rb_io_close(io);
return Qnil;
}
Closes the stream for both reading and writing if open for either or both; returns nil. See Open and Closed Streams.
If the stream is open for writing, flushes any buffered writes to the operating system before closing.
If the stream was opened by IO.popen, sets global variable $? (child exit status).
It is not an error to close an IO object that has already been closed. It just returns nil.
Example:
Example
IO.popen('ruby', 'r+') do |pipe| puts pipe.closed? pipe.close puts $? puts pipe.closed? end
Output:
false pid 13760 exit 0 true
Related: IO#close_read, IO#close_write, IO#closed?.
Ruby version changes
-
4.0
-
Reintroduce the
Fiber::Scheduler#io_closehook for asynchronousIO#close.
-
See also
#clear_screen
#closed?
#check_winsize_changed
#close_on_exec=
also in
4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0