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
ext/pty/pty.c
View on GitHub ↗
static VALUE
pty_open(VALUE klass)
{
int master_fd, slave_fd;
char slavename[DEVICELEN];
getDevice(&master_fd, &slave_fd, slavename, 1);
VALUE master_path = rb_obj_freeze(rb_sprintf("masterpty:%s", slavename));
VALUE master_io = rb_io_open_descriptor(rb_cIO, master_fd, FMODE_READWRITE | FMODE_SYNC | FMODE_DUPLEX, master_path, RUBY_IO_TIMEOUT_DEFAULT, NULL);
VALUE slave_path = rb_obj_freeze(rb_str_new_cstr(slavename));
VALUE slave_file = rb_io_open_descriptor(rb_cFile, slave_fd, FMODE_READWRITE | FMODE_SYNC | FMODE_DUPLEX | FMODE_TTY, slave_path, RUBY_IO_TIMEOUT_DEFAULT, NULL);
VALUE 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! # ... }