method
open
v1_9_3_392 -
Show latest stable
-
0 notes -
Class: PTY
- 1_8_6_287
- 1_8_7_72
- 1_8_7_330
- 1_9_1_378
- 1_9_2_180 (0)
- 1_9_3_125 (12)
- 1_9_3_392 (0)
- 2_1_10 (-38)
- 2_2_9 (0)
- 2_4_6 (6)
- 2_5_5 (0)
- 2_6_3 (0)
- What's this?
open()
public
Allocates a pty (pseudo-terminal).
In the non-block form, returns a two element array, [master_io, slave_file].
In the block form, yields two arguments 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.
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 |
Example
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. require 'io/console' # for IO#raw! m, s = PTY.open s.raw! # 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 # dependent. ret = begin m.gets # FreeBSD returns nil. rescue Errno::EIO # GNU/Linux raises EIO. nil end p ret #=> nil