Flowdock
method

open

Importance_2
open() public

Allocates a pty (pseudo-terminal).

It returns an array which contains an IO object and a File object. The former is the master of the pty. The latter is the slave of the pty.

If a block is given, it yields the array instead of return. The value of the block is returned. master_io and slave_file is closed when return if they are not closed.

The path name of the terminal device can be gotten by slave_file.path.

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

# Change the buffering type in factor command,
# assuming that factor uses stdio for stdout buffering.
# If IO.pipe is used instead of PTY.open,
# this code deadlocks because factor's stdout is fully buffered.
m, s = PTY.open
system("stty raw", :in=>s) # disable newline conversion.
r, w = IO.pipe
pid = spawn("factor", :in=>r, :out=>s)
r.close
s.close
w.puts "42"
p m.gets #=> "42: 2 3 7\n"
w.puts "144"
p m.gets #=> "144: 2 2 2 2 3 3\n"
w.close
# The result of read operation when pty slave is closed is platform dependnet.
ret = begin
        m.gets          # FreeBSD returns nil.
      rescue Errno::EIO # GNU/Linux raises EIO.
        nil
      end
p ret #=> nil
Show source
Register or log in to add new notes.