Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
IO#close_write
() -> nil
close_write → nil
io.c
View on GitHub ↗
static VALUE
rb_io_close_write(VALUE io)
{
rb_io_t *fptr;
VALUE write_io;
write_io = GetWriteIO(io);
fptr = rb_io_get_fptr(rb_io_taint_check(write_io));
if (fptr->fd < 0) return Qnil;
if (is_socket(fptr->fd, fptr->pathv)) {
#ifndef SHUT_WR
# define SHUT_WR 1
#endif
/* Flush any buffered data before shutting down the write side.
* Otherwise the buffered bytes are silently dropped here, and a
* subsequent #close would try to flush them into the now
* shutdown(SHUT_WR) socket and fail with EPIPE. This matches the
* behaviour of the non-socket path below, which flushes via
* rb_io_close(). */
if (fptr->mode & FMODE_WRITABLE) {
if (io_fflush(fptr) < 0)
rb_sys_fail_on_write(fptr);
}
if (shutdown(fptr->fd, SHUT_WR) < 0)
rb_sys_fail_path(fptr->pathv);
fptr->mode &= ~FMODE_WRITABLE;
if (!(fptr->mode & FMODE_READABLE))
return rb_io_close(write_io);
return Qnil;
}
if ((fptr->mode & (FMODE_DUPLEX|FMODE_READABLE)) == FMODE_READABLE) {
rb_raise(rb_eIOError, "closing non-duplex IO for writing");
}
if (io != write_io) {
fptr = rb_io_get_fptr(rb_io_taint_check(io));
fptr->tied_io_for_writing = 0;
}
rb_io_close(write_io);
return Qnil;
}
Closes the stream for writing if open for writing; returns nil. See Open and Closed Streams.
Flushes any buffered writes to the operating system before closing.
If the stream was opened by IO.popen and is also closed for reading, sets global variable $? (child exit status).
Example
IO.popen('ruby', 'r+') do |pipe| puts pipe.closed? pipe.close_read puts pipe.closed? pipe.close_write puts $? puts pipe.closed? end
Output:
false false pid 15044 exit 0 true
Related: IO#close, IO#close_read, IO#closed?.
See also
#close_read
#console_input_events
#close_on_exec?
#console_mode
also in
4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0