Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
PTY.getpty
static VALUE
pty_getpty(int argc, VALUE *argv, VALUE self)
{
VALUE res;
struct pty_info info;
char SlaveName[DEVICELEN];
establishShell(argc, argv, &info, SlaveName);
VALUE pty_path = rb_obj_freeze(rb_str_new_cstr(SlaveName));
VALUE rport = rb_io_open_descriptor(
rb_cFile, info.fd, FMODE_READABLE, pty_path, RUBY_IO_TIMEOUT_DEFAULT, NULL
);
int wpty_fd = rb_cloexec_dup(info.fd);
if (wpty_fd == -1) {
rb_sys_fail("dup()");
}
VALUE wport = rb_io_open_descriptor(
rb_cFile, wpty_fd, FMODE_WRITABLE | FMODE_TRUNC | FMODE_CREATE | FMODE_SYNC,
pty_path, RUBY_IO_TIMEOUT_DEFAULT, NULL
);
res = rb_ary_new2(3);
rb_ary_store(res, 0, rport);
rb_ary_store(res, 1, wport);
rb_ary_store(res,2,PIDT2NUM(info.child_pid));
if (rb_block_given_p()) {
rb_ensure(rb_yield, res, pty_detach_process, (VALUE)&info);
return Qnil;
}
return res;
}
Alias of .spawn
Spawns the specified command on a newly allocated pty. You can also use the alias ::getpty.
The command’s controlling tty is set to the slave device of the pty and its standard input/output/error is redirected to the slave device.
env is an optional hash that provides additional environment variables to the spawned pty.
# sets FOO to "bar" PTY.spawn({"FOO"=>"bar"}, "printenv", "FOO") { |r,w,pid| p r.read } #=> "bar\r\n" # unsets FOO PTY.spawn({"FOO"=>nil}, "printenv", "FOO") { |r,w,pid| p r.read } #=> ""
command and command_line are the full commands to run, given a String. Any additional arguments will be passed to the command.
Return values
In the non-block form this returns an array of size three, [r, w, pid].
In the block form these same values will be yielded to the block: