End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
class method
PTY.open
PTY.open => [master_io, slave_file]
PTY.open {|(master_io, slave_file)| ... } => block value
ext/pty/pty.c
View on GitHub ↗
static VALUE
pty_open(VALUE klass)
{
int master_fd, slave_fd;
char slavename[DEVICELEN];
VALUE master_io, slave_file;
rb_io_t *master_fptr, *slave_fptr;
VALUE assoc;
getDevice(&master_fd, &slave_fd, slavename, 1);
master_io = rb_obj_alloc(rb_cIO);
MakeOpenFile(master_io, master_fptr);
master_fptr->mode = FMODE_READWRITE | FMODE_SYNC | FMODE_DUPLEX;
master_fptr->fd = master_fd;
master_fptr->pathv = rb_obj_freeze(rb_sprintf("masterpty:%s", slavename));
slave_file = rb_obj_alloc(rb_cFile);
MakeOpenFile(slave_file, slave_fptr);
slave_fptr->mode = FMODE_READWRITE | FMODE_SYNC | FMODE_DUPLEX | FMODE_TTY;
slave_fptr->fd = slave_fd;
slave_fptr->pathv = rb_obj_freeze(rb_str_new_cstr(slavename));
assoc = rb_assoc_new(master_io, slave_file);
if (rb_block_given_p()) {
return rb_ensure(rb_yield, assoc, pty_close_pty, assoc);
}
return assoc;
}
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 viaslave_file.path
IO#raw! is usable to disable newline conversions:
Example
require 'io/console' PTY.open {|m, s| s.raw! # ... }