Skip to content
class method

IO.pipe

(?::String | ::Encoding | nil ext_or_ext_int_enc, ?::String | ::Encoding | nil int_enc, ?external_encoding: ::String | ::Encoding | nil, ?internal_encoding: ::String | ::Encoding | nil, ?encoding: ::String | ::Encoding | nil, ?textmode: ::boolish, ?binmode: ::boolish, ?autoclose: ::boolish, ?mode: ::String, ?chomp: ::boolish) -> [ ::IO, ::IO ]
[X] (?::String | ::Encoding | nil ext_or_ext_int_enc, ?::String | ::Encoding | nil int_enc, ?external_encoding: ::String | ::Encoding | nil, ?internal_encoding: ::String | ::Encoding | nil, ?encoding: ::String | ::Encoding | nil, ?textmode: ::boolish, ?binmode: ::boolish, ?autoclose: ::boolish, ?mode: ::String, ?chomp: ::boolish) { (::IO read_io, ::IO write_io) -> X } -> X
IO.pipe(**opts) → [read_io, write_io]
IO.pipe(enc, **opts) → [read_io, write_io]
IO.pipe(ext_enc, int_enc, **opts) → [read_io, write_io]
IO.pipe(**opts) {|read_io, write_io| ...} → object
IO.pipe(enc, **opts) {|read_io, write_io| ...} → object
IO.pipe(ext_enc, int_enc, **opts) {|read_io, write_io| ...} → object

Creates a pair of pipe endpoints, read_io and write_io, connected to each other.

If argument enc_string is given, it must be a string containing one of:

  • The name of the encoding to be used as the external encoding.

  • The colon-separated names of two encodings to be used as the external and internal encodings.

If argument int_enc is given, it must be an Encoding object or encoding name string that specifies the internal encoding to be used; if argument ext_enc is also given, it must be an Encoding object or encoding name string that specifies the external encoding to be used.

The string read from read_io is tagged with the external encoding; if an internal encoding is also specified, the string is converted to, and tagged with, that encoding.

If any encoding is specified, optional hash arguments specify the conversion option.

Optional keyword arguments opts specify:

With no block given, returns the two endpoints in an array:

Example
IO.pipe # => [#<IO:fd 4>, #<IO:fd 5>]

With a block given, calls the block with the two endpoints; closes both endpoints and returns the value of the block:

Example
IO.pipe {|read_io, write_io| p read_io; p write_io }

Output:

Example
#<IO:fd 6>
#<IO:fd 7>

Not available on all platforms.

In the example below, the two processes close the ends of the pipe that they are not using. This is not just a cosmetic nicety. The read end of a pipe will not generate an end of file condition if there are any writers with the pipe still open. In the case of the parent process, the rd.read will never return if it does not first issue a wr.close:

Example
rd, wr = IO.pipe

if fork
  wr.close
  puts "Parent got: <#{rd.read}>"
  rd.close
  Process.wait
else
  rd.close
  puts 'Sending message to parent'
  wr.write "Hi Dad"
  wr.close
end

produces:

Sending message to parent
Parent got: <Hi Dad>
See also .open .popen .new .read also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0