Class: Sanford::IOPipe

Inherits:
Object
  • Object
show all
Defined in:
lib/sanford/io_pipe.rb

Constant Summary collapse

NULL =
File.open('/dev/null', 'w')
NUMBER_OF_BYTES =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeIOPipe

Returns a new instance of IOPipe.



10
11
12
13
# File 'lib/sanford/io_pipe.rb', line 10

def initialize
  @reader = NULL
  @writer = NULL
end

Instance Attribute Details

#readerObject (readonly)

Returns the value of attribute reader.



8
9
10
# File 'lib/sanford/io_pipe.rb', line 8

def reader
  @reader
end

#writerObject (readonly)

Returns the value of attribute writer.



8
9
10
# File 'lib/sanford/io_pipe.rb', line 8

def writer
  @writer
end

Instance Method Details

#readObject



26
27
28
# File 'lib/sanford/io_pipe.rb', line 26

def read
  @reader.read_nonblock(NUMBER_OF_BYTES)
end

#setupObject



15
16
17
# File 'lib/sanford/io_pipe.rb', line 15

def setup
  @reader, @writer = ::IO.pipe
end

#teardownObject



19
20
21
22
23
24
# File 'lib/sanford/io_pipe.rb', line 19

def teardown
  @reader.close unless @reader === NULL
  @writer.close unless @writer === NULL
  @reader = NULL
  @writer = NULL
end

#wait(timeout = nil) ⇒ Object



34
35
36
# File 'lib/sanford/io_pipe.rb', line 34

def wait(timeout = nil)
  !!::IO.select([@reader], nil, nil, timeout)
end

#write(value) ⇒ Object



30
31
32
# File 'lib/sanford/io_pipe.rb', line 30

def write(value)
  @writer.write_nonblock(value[0, NUMBER_OF_BYTES])
end