Skip to content
instance method

IO#close

() -> nil
close → nil

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

  1. 4.0