Skip to content
class method

PTY.open

() -> [ ::IO, ::File ]
[A] () { ([ ::IO, ::File ]) -> A } -> A
PTY.open => [master_io, slave_file]
PTY.open {|(master_io, slave_file)| ... } => block value

Allocates a pty (pseudo-terminal).

In the block form, yields an array of two elements (master_io, slave_file) and the value of the block is returned from open.

The IO and File are both closed after the block completes if they haven’t been already closed.

Example
PTY.open {|master, slave|
  p master      #=> #<IO:masterpty:/dev/pts/1>
  p slave      #=> #<File:/dev/pts/1>
  p slave.path #=> "/dev/pts/1"
}

In the non-block form, returns a two element array, [master_io, slave_file].

Example
master, slave = PTY.open
# do something with master for IO, or the slave file

The arguments in both forms are:

master_io

the master of the pty, as an IO.

slave_file

the slave of the pty, as a File. The path to the terminal device is available via slave_file.path

IO#raw! is usable to disable newline conversions:

Example
require 'io/console'
PTY.open {|m, s|
  s.raw!
  # ...
}
See also .getpty .spawn .check also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0